Vue.js Using "this" in Vue


Introduction

One of the most common errors we find in Vue code on StackOverflow is the misuse of this. The most common mistakes fall generally in two areas, using this in callbacks for promises or other asynchronous functions and using arrow functions to define methods, computed properties, etc.

WRONG! Using "this" in a callback inside a Vue method.

new Vue({
  el:"#app",
  data:{
    foo: "bar"
  },
  methods:{
    doSomethingAsynchronous(){
      setTimeout(function(){
        // This is wrong! Inside this function,
        // "this" refers to the window object.
        this.foo = "baz";
      }, 1000);
    }
  }
})

WRONG! Using "this" inside a promise.

new Vue({
  el:"#star-wars-people",
  data:{
    people: null
  },
  mounted: function(){
    $.getJSON("http://swapi.co/api/people/", function(data){
      // Again, this is wrong! "this", here, refers to the window.
      this.people = data.results;
    })
  }
})

RIGHT! Use a closure to capture "this"

You can capture the correct this using a closure.

new Vue({
  el:"#star-wars-people",
  data:{
    people: null
  },
  mounted: function(){
    // Before executing the web service call, save this to a local variable
    var self = this;
    $.getJSON("http://swapi.co/api/people/", function(data){
      // Inside this call back, because of the closure, self will
      // be accessible and refers to the Vue object.
      self.people = data.results;
    })
  }
})

RIGHT! Use bind.

You can bind the callback function.

new Vue({
  el:"#star-wars-people",
  data:{
    people: null
  },
  mounted:function(){
    $.getJSON("http://swapi.co/api/people/", function(data){
      this.people = data.results;
    }.bind(this));
  }
})

RIGHT! Use an arrow function.

new Vue({
  el:"#star-wars-people",
  data:{
    people: null
  },
  mounted: function(){
    $.getJSON("http://swapi.co/api/people/", data => this.people = data.results);
  }
})

Caution! Arrow functions are a syntax introduced in Ecmascript 2015. It is not yet supported but all modern browsers, so only use it if you are targetting a browser you know supports it, or if you are compiling your javascript down to ES5 syntax using something like babel.

WRONG! Using an arrow function to define a method that refers to "this"

new Vue({
  el:"#app",
  data:{
    foo: "bar"
  },
  methods:{
    // This is wrong! Arrow functions capture "this" lexically
    // and "this" will refer to the window.
    doSomething: () => this.foo = "baz"
  }
})

RIGHT! Define methods with the typical function syntax

new Vue({
  el:"#app",
  data:{
    foo: "bar"
  },
  methods:{
    doSomething: function(){
      this.foo = "baz"
    }
  }
})

Alternatively, if you are using a javascript compiler or a browser that supports Ecmascript 2015

new Vue({
  el:"#app",
  data:{
    foo: "bar"
  },
  methods:{
    doSomething(){
      this.foo = "baz"
    }
  }
})