MOCKSTACKS
EN
Questions And Answers

More Tutorials









VueJs Using "this"

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.

Examples


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"
 }
 }
})


Conclusion

In this page (written and validated by ) you learned about VueJs Using this . What's Next? If you are interested in completing VueJs tutorial, your next topic will be learning about: VueJs single file components.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.