這個是因為沒有引入 和初始化? web3 對象。
原來的代碼:
sendBtn.onclick = async () => { try { const accounts = await ethereum.request({method:'eth_requestAccounts'}); console.info(accounts) web3.eth.sendTransaction({ from:accounts[0], to:'0x4CCbD5D055fAd49d9278a6c43F1d27b9537737b5', value: 100000000000000000, gas: 21000, gasPrice: 20000000000 },(result) =>{ console.log(result); }) }catch (e) { console.error(e) } }
現(xiàn)在初始化 web3
sendBtn.onclick = async () => { try { const accounts = await ethereum.request({method:'eth_requestAccounts'}); // 初始化 web3 web3 = new Web3(web3.currentProvider); if (web3.currentProvider.isMetaMask == true) { console.info("MetaMask可用") } else { console.info("非MetaMask環(huán)境") } web3.eth.sendTransaction({ from:accounts[0], to:'0x4CCbD5D055fAd49d9278a6c43F1d27b9537737b5', value: 100000000000000000, gas: 21000, gasPrice: 20000000000 },(result) =>{ console.log(result); }) }catch (e) { console.error(e) } }
然后執(zhí)行,后發(fā)現(xiàn)?Web3 is not defined 錯誤,文章來源:http://www.zghlxwxcb.cn/news/detail-756384.html
現(xiàn)在只要引入 web3.js 文件即可。文章來源地址http://www.zghlxwxcb.cn/news/detail-756384.html
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
到了這里,關于Cannot read properties of undefined (reading ‘sendTransaction‘)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!