鍍金池/ 問(wèn)答/網(wǎng)絡(luò)安全  HTML/ vue2 設(shè)定從服務(wù)器獲取的值,為什么總是提示undefined呢?

vue2 設(shè)定從服務(wù)器獲取的值,為什么總是提示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) {
                            // 請(qǐng)求已發(fā)出,但服務(wù)器響應(yīng)的狀態(tài)碼不在 2xx 范圍內(nèi)
                            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

總是提示說(shuō)沒(méi)有定義這個(gè)屬性,請(qǐng)問(wèn)這個(gè)是什么問(wèn)題,TKS

回答
編輯回答
怪痞

this.report = response.data;這里 this 沒(méi)有指向 Vue 實(shí)例.
回調(diào)函數(shù)改成箭頭函數(shù); 或者在 fetchCustomers函數(shù)頂部let that = this 保存 this 指向, 然后 that.report = response.data;

2017年6月6日 12:01