vue2 设定从服务器获取的值,为什么总是提示undefined呢?

data () {
            return {
                report: ''
            }
        },
        methods:{
            fetchCustomers(){
                this.$axios.get("http://api.xxxx.com/admin/report")
                    .then(function(response){
                        console.log(response.data)
                        this.report = response.data;
                        console.log(this.report)
                    })
                    .catch(function (error) {
                        if (error.response) {
                            // 请求已发出,但服务器响应的状态码不在 2xx 范围内
                            console.log(error.response.data);
                            console.log(error.response.status);
                            console.log(error.response.headers);
                        } else {
                            // Something happened in setting up the request that triggered an Error
                            console.log('Error', error.message);
                        }
                        console.log(error.config);
                    });
            }
        }

clipboard.png

总是提示说没有定义这个属性,请问这个是什么问题,TKS

阅读 3.8k
1 个回答

this.report = response.data;这里 this 没有指向 Vue 实例.
回调函数改成箭头函数; 或者在 fetchCustomers函数顶部let that = this 保存 this 指向, 然后 that.report = response.data;

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题