这个是因为没有引入 和初始化 web3 对象。

原来的代码:

sendBtn.onclick = async () => {try {constaccounts = awaitethereum.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)}}

现在初始化 web3

sendBtn.onclick = async () => {try {constaccounts = awaitethereum.request({method:'eth_requestAccounts'});// 初始化 web3 web3 = new Web3(web3.currentProvider);if (web3.currentProvider.isMetaMask == true) {console.info("MetaMask可用")} else {console.info("非MetaMask环境")}web3.eth.sendTransaction({from:accounts[0],to:'0x4CCbD5D055fAd49d9278a6c43F1d27b9537737b5',value: 100000000000000000,gas: 21000,gasPrice: 20000000000},(result) =>{console.log(result);})}catch (e) {console.error(e)}}

然后执行,后发现Web3 is not defined 错误,

现在只要引入 web3.js 文件即可。