首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >window.ethereum

window.ethereum
EN

Ethereum用户
提问于 2020-04-01 18:46:40
回答 2查看 3.1K关注 0票数 4

当我创建web3实例并运行index.html时,当我从控制台调用它时,没有定义ethereum,我已经安装了元问题并使用了ropsten network.Why,但当我像加密器一样在站点上运行时,它工作得非常完美。

代码语言:javascript
复制
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()
EN

回答 2

Ethereum用户

回答已采纳

发布于 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)。

票数 6
EN

Ethereum用户

发布于 2021-05-06 17:11:00

对我来说,一种更简单的方法是为VSCode安装Live扩展(仅限VsCode用户)

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/82073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档