我一直在尝试将Web 3安装到我的Ionic v4项目中。当我为项目提供服务时,它一直抛出错误。我收到一个错误,指出引用错误:未定义全局。
1.开始新的Ionic项目
1.安装web 3:npm安装--保存web 3
1.安装节点类型:npm安装--保存设备@类型/节点
1.编辑一些代码来访问web 3:
import { Component } from '@angular/core';
import { Web3 } from 'web3';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/v3/apikey...'));
constructor() {}
test(){
console.log(this.web3);
}
}
1.运行离子服务器
1.编译成功,但我在谷歌浏览器的控制台像这样的错误
core.js:9110 ERROR Error: Uncaught (in promise): ReferenceError: global is not defined
ReferenceError: global is not defined
at Object../node_modules/stream-http/lib/capability.js (capability.js:1)
at __webpack_require__ (bootstrap:84)
at Object../node_modules/stream-http/lib/request.js (request.js:1)
at __webpack_require__ (bootstrap:84)
at Object../node_modules/stream-http/index.js (index.js:1)
at __webpack_require__ (bootstrap:84)
at Object../node_modules/xhr2-cookies/dist/xml-http-request.js (xml-http-request.js:21)
at __webpack_require__ (bootstrap:84)
at Object../node_modules/xhr2-cookies/dist/index.js (index.js:6)
at __webpack_require__ (bootstrap:84)
at resolvePromise (zone-evergreen.js:797)
at resolvePromise (zone-evergreen.js:754)
at zone-evergreen.js:858
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:34182)
at ZoneDelegate.invokeTask (zone-evergreen.js:390)
at Zone.runTask (zone-evergreen.js:168)
at drainMicroTaskQueue (zone-evergreen.js:559)
package.json
{
"name": "my-app",
"version": "0.0.1",
"author": "Ionic Framework",
"homepage": "https://ionicframework.com/",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/common": "~8.1.2",
"@angular/compiler": "~8.1.2",
"@angular/core": "~8.1.2",
"@angular/forms": "~8.1.2",
"@angular/platform-browser": "~8.1.2",
"@angular/platform-browser-dynamic": "~8.1.2",
"@angular/router": "~8.1.2",
"@ionic-native/core": "^5.0.0",
"@ionic-native/splash-screen": "^5.0.0",
"@ionic-native/status-bar": "^5.0.0",
"@ionic/angular": "^4.7.1",
"core-js": "^2.5.4",
"rxjs": "~6.5.1",
"tslib": "^1.9.0",
"web3": "^1.2.0",
"zone.js": "~0.9.1"
},
"devDependencies": {
"@angular-devkit/architect": "~0.801.2",
"@angular-devkit/build-angular": "~0.801.2",
"@angular-devkit/core": "~8.1.2",
"@angular-devkit/schematics": "~8.1.2",
"@angular/cli": "~8.1.2",
"@angular/compiler": "~8.1.2",
"@angular/compiler-cli": "~8.1.2",
"@angular/language-service": "~8.1.2",
"@ionic/angular-toolkit": "~2.0.0",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^8.9.5",
"codelyzer": "^5.0.0",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.15.0",
"typescript": "~3.4.3"
},
"description": "An Ionic project"
}
我在这里找到了一个建议https://github.com/ethereum/web3.js/issues/2260#issuecomment-458519127。我遵循了每一步,但我也得到了同样的错误。
如何将web 3导入到Ionic项目中?
3条答案
按热度按时间izj3ouym1#
我在WalletConnect上遇到了这个问题。这是我最终得到的解决方案:
我只是导入钱包连接通过:
为了让这些类型正常工作,我创建了一个d.ts文件:
niknxzdl2#
不确定离子有自己的polyfill,但你可以添加到您的polyfill
2skhul333#
如果再出现一个错误Uncatch ReferenceError:进程未定义,则添加process.env