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

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) {
                            // 請求已發(fā)出,但服務器響應的狀態(tài)碼不在 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

回答
編輯回答
怪痞

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

2017年6月6日 12:01