I need to update a Vue property in a Firebase callback as follow, but it's not working. This code
methods: {
sign_out : function(e) {
this.item_count = 0
}
}
works, but when the property is set in a promise callback:
methods: {
sign_out : function(e) {
firebase.auth().signOut().then(function() {
this.item_count = 0
})
},
How can I set a property's value in this case?
One way is to make a reference to this outside of the callback. Use the reference inside the callback to access the this on the intended object.
So try something like:
Your
this
in your callback is pointing to the wrong object. There are a few ways you can fix this.Capture
this
in a closure.Use a fat arrow.
Use bind().
Fat arrows will not work in all modern browsers yet, so only use them if you are compiling to es5.