简体   繁体   English

“EthereumProvider”类型上不存在属性“on”

[英]Property 'on' does not exist on type 'EthereumProvider'

I'm trying to basically use the window.ethereum provider to interact with Metamask but can't set event listeners just as mentioned in the Metamask doc.我试图基本上使用 window.ethereum 提供程序与 Metamask 交互,但不能像 Metamask 文档中提到的那样设置事件侦听器。 I'm literally just doing this in a TypeScript class constructor: window.ethereum?.on('accountsChanged', this.handleAccountChange);我实际上只是在 TypeScript class 构造函数中执行此操作: window.ethereum?.on('accountsChanged', this.handleAccountChange);

The window.ethereum provider seems ok for most of the stuff but I get that error (the title) in VSCode. window.ethereum 提供程序似乎对大部分内容都没有问题,但我在 VSCode 中遇到了该错误(标题)。

In the console when I log window.ethereum I indeed can't seen some 'on' stuff:在控制台中,当我登录 window.ethereum 时,我确实看不到一些“on”的东西:

{_events: {…}, _eventsCount: 0, _maxListeners: 100, _log: u, _state: {…}, …}
chainId
: 
"0x89"
enable
: 
ƒ ()
id
: 
"MetaMask"
isMetaMask
: 
true
networkVersion
: 
"137"
request
: 
ƒ ()
selectedAddress
: 
"0xf2dd55f59256bd65f41d2517c27a8b5fa780a18b"
send
: 
ƒ ()
sendAsync
: 
ƒ ()
_events
: 
{}
_eventsCount
: 
0
_handleAccountsChanged
: 
ƒ ()
_handleChainChanged
: 
ƒ ()
_handleConnect
: 
ƒ ()
_handleDisconnect
: 
ƒ ()
_handleStreamDisconnect
: 
ƒ ()
_handleUnlockStateChanged
: 
ƒ ()
_jsonRpcConnection
: 
{events: o, stream: d, middleware: ƒ}
_log
: 
u {name: undefined, levels: {…}, methodFactory: ƒ, getLevel: ƒ, setLevel: ƒ, …}
_maxListeners
: 
100
_metamask
: 
Proxy {isUnlocked: ƒ, requestBatch: ƒ}
_rpcEngine
: 
s {_events: {…}, _eventsCount: 0, _maxListeners: undefined, _middleware: Array(4)}
_rpcRequest
: 
ƒ ()
_sendSync
: 
ƒ ()
_sentWarnings
: 
{enable: false, experimentalMethods: false, send: false, events: {…}}
_state
: 
accounts
: 
['0xf2dd55f59256bd65f41d2517c27a8b5fa780a18b']
initialized
: 
true
isConnected
: 
true
isPermanentlyDisconnected
: 
false
isUnlocked
: 
true
[[Prototype]]
: 
Object
_warnOfDeprecation
: 
ƒ ()
[[Prototype]]
: 
Object

Any idea?任何的想法? I'm lost for now...我暂时迷路了...

your app is not connected to the metamask.您的应用未连接到元掩码。 Click on metamask extension and you should see this pop up单击元掩码扩展,您应该会看到此弹出窗口

在此处输入图像描述

you have to manually connect你必须手动连接

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

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