Angular 角度8:“;ReferenceError:未定义全局;当我';我正在尝试将客户端应用程序与以太坊blckchain连接起来
我正在使用注入令牌web3.ts设置我的web3提供程序,该令牌由app.component.ts导入并用于async ngOnInit() 我已经试过了: 它向我显示了同样的错误。 我也试过: 当然,它也显示了同样的错误 web3.ts:Angular 角度8:“;ReferenceError:未定义全局;当我';我正在尝试将客户端应用程序与以太坊blckchain连接起来,angular,typescript,ethereum,solidity,Angular,Typescript,Ethereum,Solidity,我正在使用注入令牌web3.ts设置我的web3提供程序,该令牌由app.component.ts导入并用于async ngOnInit() 我已经试过了: 它向我显示了同样的错误。 我也试过: 当然,它也显示了同样的错误 web3.ts: import { InjectionToken } from '@angular/core'; import Web3 from 'web3'; export const WEB3 = new InjectionToken<Web3>('web
import { InjectionToken } from '@angular/core';
import Web3 from 'web3';
export const WEB3 = new InjectionToken<Web3>('web3', {
providedIn: 'root',
factory: () => {
try {
const provider = ('ethereum' in window) ? window['ethereum'] : Web3.givenProvider;
return new Web3(provider);
} catch (err) {
throw new Error('Non-Ethereum browser detected. You should consider trying Mist or MetaMask!');
}
}
});
---控制台错误---
我认为你应该像这样连接到提供商
if (typeof window.web3 !== 'undefined') {
this.web3 = new Web3(window.web3.currentProvider);
} else {
console.log('No web3? You should consider trying MetaMask!');
Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send;
this.web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
}
我认为你应该像这样连接到提供商
if (typeof window.web3 !== 'undefined') {
this.web3 = new Web3(window.web3.currentProvider);
} else {
console.log('No web3? You should consider trying MetaMask!');
Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send;
this.web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
}
每当我使用
web3
关键字时,我都会遇到同样的错误,在阅读了一些文章后,我得到了答案
在src
内的文件polyfill.js
中添加代码段
(window as any).global = window;
global.Buffer = global.Buffer || require('buffer').Buffer;
global.process = require('process');
首先,您可以尝试只添加代码的第一行,这很可能解决了窗口的全局错误global is not defined
我添加了其他代码行,因为在解决全局错误后,很可能会出现新的错误缓冲区未定义
添加第二行以解决此错误后,将出现与流程相关的新错误未定义流程
出现这些错误后,无法读取未定义的属性“slice”
但对我来说,这些代码完成了所有的工作
更多有关:
每当我使用web3
关键字时,我都会遇到同样的错误,在阅读了一些文章后,我得到了答案
在src
内的文件polyfill.js
中添加代码段
(window as any).global = window;
global.Buffer = global.Buffer || require('buffer').Buffer;
global.process = require('process');
首先,您可以尝试只添加代码的第一行,这很可能解决了窗口的全局错误global is not defined
我添加了其他代码行,因为在解决全局错误后,很可能会出现新的错误缓冲区未定义
添加第二行以解决此错误后,将出现与流程相关的新错误未定义流程
出现这些错误后,无法读取未定义的属性“slice”
但对我来说,这些代码完成了所有的工作
更多有关: