问题描述:
在练习搭建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/
,如需转载,请注明出处,否则将追究法律责任。