Watcher doesn’t support lambda functions in Vue

Sami C.
Sep 20, 2021

--

The reason that you can’t execute the following code

watch: {     
a: (val, oldVal) => {
this.executeMyMethod()
}
}

is because arrow functions bind to the parent context, so ‘this’ will not be the vue context as you expect, this.executeMyMethod will be undefined.

This is how your code should look like:

watch: {     
a: function(val, oldVal){
this.executeMyMethod()
}
}

Enjoy!

--

--

Sami C.
Sami C.

Written by Sami C.

Freelance Software Engineer

Responses (1)