Skip to content
Advertisement

Variable declared in data() is Undefined in Listener

I am declaring a “userid” variable in the data() section of my component. Within a mounted() method, I am initiating a listener for MetaMask. Upon changing account in MetaMask this listener is triggered. However, the “userid” (declared in data()) within the listener is undefined.

JavaScript

How can I solve this problem?

Advertisement

Answer

Vue runs in strict mode. That means that this is binded to regular function itself.

You have 3 ways to solve this problem:

Use arrow function:

JavaScript

Use .bind()

JavaScript

Declare a variable outside and assign this to it:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement