Wie kann ich web3.js mit Angular 2 verwenden

Ich versuche, Web3in meine Komponentendateien zu importieren, was darin geschrieben ist, Typescriptaber ich konnte es nicht schaffen.

Ich habe verwendet npm install --save web3-typescript-typingsund hinzugefügt

"include": [
    "../node_modules/web3-typescript-typings/index.d.ts"
  ]

zu tsconfig.json. Ich bekomme jedoch immer noch Fehler von der Browserkonsole:

Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/node_modules/web3
    Error: XHR error (404 Not Found) loading http://localhost:3000/node_modules/web3
        at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (http://localhost:3000/node_modules/zone.js/dist/zone.js:1056:39)
        at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:424:31)
        at Zone.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:191:47)
        at XMLHttpRequest.ZoneTask.invoke (http://localhost:3000/node_modules/zone.js/dist/zone.js:486:38)
    Error loading http://localhost:3000/node_modules/web3 as "@web3" from http://localhost:3000/app/exchange.component.js
        at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (http://localhost:3000/node_modules/zone.js/dist/zone.js:1056:39)
        at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:424:31)
        at Zone.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:191:47)
        at XMLHttpRequest.ZoneTask.invoke (http://localhost:3000/node_modules/zone.js/dist/zone.js:486:38)
    Error loading http://localhost:3000/node_modules/web3 as "@web3" from http://localhost:3000/app/exchange.component.js
(anonymous) @ (index):18
ZoneDelegate.invoke @ zone.js:391
Zone.run @ zone.js:141
(anonymous) @ zone.js:818
ZoneDelegate.invokeTask @ zone.js:424
Zone.runTask @ zone.js:191
drainMicroTaskQueue @ zone.js:584
ZoneTask.invoke @ zone.js:490

Kann mir jemand Licht ins Dunkel bringen?

Antworten (2)

Du bist eigentlich fast da. Stellen Sie sicher, dass Sie das Modul wie folgt in Ihre Komponente importieren:import Web3 from 'web3';

Verbinden Sie sich dann mit dem Netzwerk:web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

Dann benutze es so:

this.web3.isConnected()

Für web3 1.0.0 & Angular 5 Web3funktioniert das Importieren in eine Komponente ohne Installation/ Einbindungweb3-typescript-typings