If i try set variable in vue2 when axios error i get this: Uncaught (in promise) TypeError: Cannot set properties of undefined (setting ‘snackbar’)
axios.post('/testaxios', {
}).then(response => {
console.log(response.data);
}).catch(function (error) {
console.error(error);
this.snackbar = true;
});
data() {
return {
snackbar: false,
}
}
>Solution :
You either need to bind the catch function to the this context:
catch(function(error) {
console.error(error);
this.snackbar = true;
}).bind(this);
Or simply use an arrow function (which does the binding automatically):
catch((error) => {
console.error(error);
this.snackbar = true;
});