Vue.js Vue请求可以';无法获取组件数据

Vue.js Vue请求可以';无法获取组件数据,vue.js,vuejs2,Vue.js,Vuejs2,当HTTP请求返回错误时,我试图禁用加载,但它给了我这个错误 未捕获(承诺中)TypeError:无法设置未定义的属性“加载” 代码 导出默认值{ 名称:'登录', 数据(){ 返回{ 加载:false,//请求错误时无法设置为false } }, 方法:{ 登录:函数(){ 这是。加载=真; const{username,password}=this 这个.$store.dispatch('login',{username,password})。然后((resp)=>{ 设置超时(()=>{

当HTTP请求返回错误时,我试图禁用加载,但它给了我这个错误

未捕获(承诺中)TypeError:无法设置未定义的属性“加载”

代码
导出默认值{
名称:'登录',
数据(){
返回{
加载:false,//请求错误时无法设置为false
}
},
方法:{
登录:函数(){
这是。加载=真;
const{username,password}=this
这个.$store.dispatch('login',{username,password})。然后((resp)=>{
设置超时(()=>{
这一点:加载=错误;
}, 5000);
//这是.$router.push({name:'dashboard'})
}).catch(函数(错误){
this.loading=false;//未捕获(承诺中)TypeError:无法设置未定义的属性“loading”
console.log('error',error);
});
}
}
}

有什么想法吗?
表示在catch方法中调用函数的对象,而不是当前的vue组件

因此,您可以在外部使用
var vm=this
并在后面使用,如下所示

  login: function () {
        this.loading = true;
        var vm = this;
        .....
        .....
        }).catch(function (error) {
            vm.loading = false;
            console.log('error', error);
        });
    }
或者使用箭头法

  login: function () {
        .....
        .....
        }).catch((error) => {
            this.loading = false;
            console.log('error', error);
        });
    }
您已经在下面的代码中使用了带有setTimeout的arrow方法,所以我想您已经意识到了这一点,也可以使用它来解决这个问题

setTimeout(() => {
    this.loading = false;
}, 5000);

表示在catch方法中调用函数的对象,而不是当前vue组件

因此,您可以在外部使用
var vm=this
并在后面使用,如下所示

  login: function () {
        this.loading = true;
        var vm = this;
        .....
        .....
        }).catch(function (error) {
            vm.loading = false;
            console.log('error', error);
        });
    }
或者使用箭头法

  login: function () {
        .....
        .....
        }).catch((error) => {
            this.loading = false;
            console.log('error', error);
        });
    }
您已经在下面的代码中使用了带有setTimeout的arrow方法,所以我想您已经意识到了这一点,也可以使用它来解决这个问题

setTimeout(() => {
    this.loading = false;
}, 5000);

这回答了你的问题吗?这回答了你的问题吗?