VueJS: variable is undefined inside computed only
Don't use arrow function ()=>{}
for computed
, it will cause the wrong context (not current Vue instance).
Change to function () {}
then it should work fine.
And for methods
, watch
, you should follow same rules.
computed: {
dummyText: function () { // change to function () {}
if (this.address.length > 0 && this.nothingFound) { // This will return error
return 'There is no such address'
} else {
return 'Keep typing'
}
}
},
You can also use es2015 shorthand for a method function:
computed: {
dummyText() {
return this.address.length > 0 && this.nothingFound ? 'There is no such address' : 'Keep typing';
}
}