我创建了两个节点。我把一个帐户连接到元询问。我将智能契约与节点上的混合连接起来。现在,我正试图将前端与web3的智能合同连接起来。在index.html中,我写道:
if ( web3 !== 'undefined') { web3 =新的Web3(web3.currentProvider);
//从Web3.providers web3 =newWeb3(新Web3)中设置您想要的提供者
然后我在浏览器th index.html中运行,但我没有按任何按钮。
发布于 2019-06-26 05:30:59
Panos,将此代码添加到您的javascript文件中,并从元询问中将网络更改到您的自定义RPC中。之后,您将从浏览器连接到本地节点。
window.addEventListener('load', async () => {
// Modern dapp browsers...
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
// Request account access if needed
await ethereum.enable();
web3.eth.getAccounts(function(err, accounts){
if(!err){
alert('Your Metamask account is: ' + accounts[0]);
}
})
} catch (error) {
// User denied account access...
}
}
// Legacy dapp browsers...
else if (window.web3) {
window.web3 = new Web3(web3.currentProvider);
web3.eth.getAccounts(function(err, accounts){
if(!err){
alert('Your Metamask account is: ' + accounts[0]);
}
})
}
// Non-dapp browsers...
else {
console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
}
});https://ethereum.stackexchange.com/questions/72232
复制相似问题