Web3js
如何在 Angular 2 中使用 web3.js
我正在嘗試導入
Web3
我編寫的組件文件,Typescript
但我無法做到。我使用
npm install --save web3-typescript-typings
並添加了"include": [ "../node_modules/web3-typescript-typings/index.d.ts" ]
到
tsconfig.json
. 但是我仍然從瀏覽器的控制台收到錯誤: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
誰能給我點燈?
你實際上就快到了。確保在組件中導入模組,如下所示:
import Web3 from 'web3';
然後,連接到網路:
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
然後像這樣使用它:
this.web3.isConnected()
對於 web3 1.0.0 和 Angular 5,導入
Web3
組件無需安裝/包含web3-typescript-typings