Answer the question
In order to leave comments, you need to log in
Why is the variable from the second anonymous function not visible?
Hi all.
I use vue.js, there is this piece of code:
props: ['user'],
computed: {
incomingActive: function () {
let user_name = this.user;
this.messages.forEach(function (message) {
alert('user_name is: ' + user_name); // <-- обращение к переменной здесь
/* ... */
return incomingActive;
});
}
},
Answer the question
In order to leave comments, you need to log in
In your case, you're losing this, which is equal to the component's instance. To "not lose" it, use an arrow function.
From documentation :
computed: {
incomingActive() {
this.messages.forEach((message) => {
alert('user_name is: ' + this.user);
/* ... */
return incomingActive;
});
},
},
ryanmorr.com/understanding-scope-and-context-in-ja...
https://habrahabr.ru/post/149516/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question