当我创建web3实例并运行index.html时,当我从控制台调用它时,没有定义ethereum,我已经安装了元问题并使用了ropsten network.Why,但当我像加密器一样在站点上运行时,它工作得非常完美。
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new
Web3.providers.HttpProvider("https://ropsten.infura.io/v3/148bee2b5da148a7b77a83f7504d00e7"));
}
async function asyncCall() {
ethereum.enable() // =========== RETURN UNDEFINED
console.log('calling');
const result = await web3.eth.getAccounts();
console.log(result);
// expected output: 'resolved'
}
asyncCall()发布于 2020-04-18 18:32:31
我也遇到了同样的问题。
显然,没有人想让解决方案变得显而易见,但我终于找到了本期,这使我来到了此链接:
由于浏览器安全限制,我们无法与运行在file://.上的dapps通信请使用本地服务器进行开发。
因此,在本地设置一个实际的服务器可以解决这个问题。我只是在安装http-服务器之后验证了这一点。
我使用npm install http-server -g安装它(假设安装了节点)。
然后使用http-server "C:\directory_of_my_html_file" -a 127.0.0.1运行它,然后能够成功地访问window.ethereum (浏览到http://localhost:8080/index.html)。
发布于 2021-05-06 17:11:00
对我来说,一种更简单的方法是为VSCode安装Live扩展(仅限VsCode用户)
https://ethereum.stackexchange.com/questions/82073
复制相似问题