将 Web3 导入离子项目时出现问题?



我一直在尝试将 Web3 导入 Ionic v4 项目。

我在 :

系统版本: macOS 莫哈韦 10.14.6

NPM:6.9.0

节点 : 10.16.1

重现步骤:

  1. 开始一个新的离子项目: ionic start my-app blank --type=angular

  2. 安装 Web3 : npm install --save web3

  3. 安装 @types/web3 : npm install --save @types/web3

  4. 添加一些代码:

import { Component } from '@angular/core';
import { Web3 } from 'web3';
@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  w = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/v3/...apikey...'));
  constructor() {}
  test(){
    console.log(this.w);
  }
}

此处错误:

import { Web3 } from 'web3';

错误日志:

模块 '"../../../../../../../../../用户/管理员/桌面/Learn_Ionic/区块链/我的应用程序/node_modules/@types/web3"' 没有导出的成员 'Web3'

这个模块只能通过打开 'allowSyntheticDefaultImports' 标志并引用其默认导出来引用 ECMAScript 导入/导出。

这是我的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",
    "@types/web3": "^1.0.19",
    "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",
    "cordova-plugin-device": "^2.0.2",
    "cordova-plugin-ionic-keyboard": "^2.1.3",
    "cordova-plugin-ionic-webview": "^4.1.1",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-plugin-whitelist": "^1.3.3",
    "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",
  "cordova": {
    "plugins": {
      "cordova-plugin-whitelist": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-device": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-ionic-webview": {
        "ANDROID_SUPPORT_ANNOTATIONS_VERSION": "27.+"
      },
      "cordova-plugin-ionic-keyboard": {}
    },
    "platforms": []
  }
}

https://github.com/ethereum/web3.js/issues/2260#issuecomment-458519127

这可能有助于您解决问题,您所要做的就是在应用程序的根目录中创建一个名为 patch.js 的文件,如上述问题注释中所述,并在 package.json 的脚本部分下添加一个脚本postinstall": "node patch.js

再次运行npm install,看看它是否有效!

相关内容

  • 没有找到相关文章

最新更新