[英]Web3 metamask connection Cannot read properties of undefined (reading 'ethereum')
I have a react application in which I am trying to connect to metamask extension via web3.我有一个反应应用程序,我试图在其中通过 web3 连接到元掩码扩展。 In my thunk.ts
, I am calling the required functions but I am getting a type error that I cannot read the properties of undefined(reading 'ethereum').在我的thunk.ts
,我正在调用所需的函数,但我收到一个类型错误,我无法读取未定义的属性(读取“以太坊”)。 Here is the code that I am using这是我正在使用的代码
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 !");
}
};
I solved it by using the following code我使用以下代码解决了它
declare global {
interface Window {
ethereum?: any;
web3?:any
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.