91

I'm creating a component with Vue.js.

When I reference this in any of the the lifecycle hooks (created, mounted, updated, etc.) it evaluates to undefined:

mounted: () => {
  console.log(this); // logs "undefined"
},

The same thing is also happening inside my computed properties:

computed: {
  foo: () => { 
    return this.bar + 1; 
  } 
}

I get the following error:

Uncaught TypeError: Cannot read property 'bar' of undefined

Why is this evaluating to undefined in these cases?

tony19
  • 125,647
  • 18
  • 229
  • 307
thanksd
  • 54,176
  • 22
  • 157
  • 150
  • 1
    Does this answer your question? [Use arrow function in vue computed does not work](https://stackoverflow.com/questions/42971081/use-arrow-function-in-vue-computed-does-not-work) – AlexMA Mar 28 '20 at 19:35

4 Answers4

179

Both of those examples use an arrow function () => { }, which binds this to a context different from the Vue instance.

As per the documentation:

Don’t use arrow functions on an instance property or callback (e.g. vm.$watch('a', newVal => this.myMethod())). As arrow functions are bound to the parent context, this will not be the Vue instance as you’d expect and this.myMethod will be undefined.

In order to get the correct reference to this as the Vue instance, use a regular function:

mounted: function () {
  console.log(this);
}

Alternatively, you can also use the ECMAScript 5 shorthand for a function:

mounted() {
  console.log(this);
}
tony19
  • 125,647
  • 18
  • 229
  • 307
thanksd
  • 54,176
  • 22
  • 157
  • 150
  • Thank you! It was so obvious and so useful at the same time. Felt like just an upvote wasn't enough! – Marcelo Gaia Oct 04 '18 at 00:06
  • Do you know why I had to use the inverse (switch from `function` to `arrow function`) to be able to use `this` inside the `then()` callback? https://github.com/Inndy/vue-clipboard2#sample-2 – Nicke Manarin Apr 26 '20 at 04:45
  • @NickeManarin the function you pass as the callback for `then` has its own `this`, so to instead have `this` reference the parent's context, you can use an arrow function. See this post: https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback – thanksd Apr 26 '20 at 16:12
  • It actually works , can someone note the differences between `function()` and `()=>` ? – Spyros Mourelatos Apr 06 '21 at 10:28
17

You are using arrow functions.

The Vue Documentation clearly states not to use arrow functions on a property or callback.

Unlike a regular function, an arrow function does not bind this. Instead, this is bound lexically (i.e. this keeps its meaning from its original context).

var instance = new  Vue({
    el:'#instance',
  data:{
    valueOfThis:null
  },
  created: ()=>{
    console.log(this)
  }
});

This logs the following object in the console:

Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}

Whereas... If we use a regular function (which we should on a Vue instance)

var instance = new  Vue({
    el:'#instance',
  data:{
    valueOfThis:null
  },
  created: function(){
    console.log(this)
  }
});

Logs the following object in the console:

hn {_uid: 0, _isVue: true, $options: {…}, _renderProxy: hn, _self: hn, …}

tony19
  • 125,647
  • 18
  • 229
  • 307
Ashutosh Narang
  • 405
  • 4
  • 8
0

If you want to keep using the arrow function, you could pass the component instance (this) as parameter like :

computed: {
  foo: (vm) => { //vm refers to this 
    return vm.bar + 1; 
  } 
}
Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164
-1

you cannot use the arrow funtion if you want to use the this. Because the arrow funtion doesn't bind this.

Sagar
  • 1