我如何在 catch error axios 中使用 set/change 变量?
How i can set/change variable in catch error axios?
如果我在 axios 错误时尝试在 vue2 中设置变量,我得到这个:未捕获(承诺)TypeError:无法设置未定义的属性(设置 'snackbar')
axios.post('/testaxios', {
}).then(response => {
console.log(response.data);
}).catch(function (error) {
console.error(error);
this.snackbar = true;
});
data() {
return {
snackbar: false,
}
}
您需要将 catch 函数绑定到 this
上下文:
catch(function(error) {
console.error(error);
this.snackbar = true;
}).bind(this);
或者简单地使用箭头函数(自动绑定):
catch((error) => {
console.error(error);
this.snackbar = true;
});
如果我在 axios 错误时尝试在 vue2 中设置变量,我得到这个:未捕获(承诺)TypeError:无法设置未定义的属性(设置 'snackbar')
axios.post('/testaxios', {
}).then(response => {
console.log(response.data);
}).catch(function (error) {
console.error(error);
this.snackbar = true;
});
data() {
return {
snackbar: false,
}
}
您需要将 catch 函数绑定到 this
上下文:
catch(function(error) {
console.error(error);
this.snackbar = true;
}).bind(this);
或者简单地使用箭头函数(自动绑定):
catch((error) => {
console.error(error);
this.snackbar = true;
});