'web3未定义',即使安装了元掩码并使用地址登录。这适用于在线编辑器,如codepen和JSFiddle.Tried window.web3,结果相同。但是,window.Web3正在返回一个函数。有什么不同,我错在哪里?
window.addEventListener("load", function() {
if (typeof web3 !== "undefined") {
web3 = new Web3(web3.currentProvider);
console.log("Pulling metamas as current provider");
web3.eth.getAccounts(function(error, accounts) {
if (!error) {
getAccountBalance(accounts[0])
} else {
console.error(error);
}
});
}else {
console.log("No metamask ? Pulling kovan testnet as current provider");
web3 = new Web3(new Web3.providers.HttpProvider("https://kovan.infura.io"));
}
});
答案 0 :(得分:0)
您需要导入Web3库,因此请使用以下代码:
const Web3 = require('web3')
然后,您可以正常使用web3 = new Web3(...)
。