ITPub博客

首页 > 区块链 > 区块链 > 编写DAPP无法连接到metamask

编写DAPP无法连接到metamask

区块链 作者:Vx19927655815 时间:2020-12-05 11:08:48 0 删除 编辑

问题描述:

在练习搭建DAPP的过程中,一直无法连接到metamask:

原因分析:

2018年metamask更新需要更新方法。https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8

解决方案:

在index.html加入

  <script>
    window.addEventListener('load', async () => {
        // Modern dapp browsers...
        if (window.ethereum) {
            window.web3 = new Web3(ethereum);
            try {
                // Request account access if needed
                await ethereum.enable();
                // Acccounts now exposed
                web3.eth.sendTransaction({/* ... */});
            } catch (error) {
                // User denied account access...
            }
        }
        // Legacy dapp browsers...
        else if (window.web3) {
            window.web3 = new Web3(web3.currentProvider);
            // Acccounts always exposed
            web3.eth.sendTransaction({/* ... */});
        }
        // Non-dapp browsers...
        else {
            console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
        }
    })
    </script>


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69990031/viewspace-2739624/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-12-04

  • 博文量
    11
  • 访问量
    6930