繁体   English   中英

Web3 元掩码连接无法读取未定义的属性(读取“以太坊”)

[英]Web3 metamask connection Cannot read properties of undefined (reading 'ethereum')

我有一个反应应用程序,我试图在其中通过 web3 连接到元掩码扩展。 在我的thunk.ts ,我正在调用所需的函数,但我收到一个类型错误,我无法读取未定义的属性(读取“以太坊”)。 这是我正在使用的代码



if (window.ethereum) {//the error line
      window.web3 = new Web3(window.ethereum);

    try {
      await window.ethereum.enable();

      updateAddress(dispatch);
    } catch (err) {
      alert("Something went wrong.");
    }
  } else if (window.web3) {
    window.web3 = new Web3(window.web3.currentProvider);
    updateAddress(dispatch);
  } else {
    alert("You have to install MetaMask !");
  }
};

这是错误截图
这是错误截图

我使用以下代码解决了它

declare global {
  interface Window {
    ethereum?: any;
    web3?:any
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM