TypeError: Cannot set property 'options' of undefined

 

axios調用API返回的數據賦值給options,報錯TypeError: Cannot set property 'options' of undefined
    axios.get('/api/ServerInfo/GetQueryTypedTSSST'
    ).then(function(res){

        this.options=res.data
       
    }).catch(function (error) {
        console.log(error);
    });

 

可是在組件中已經聲明瞭

    data() {
      return {
        options:[],

 

在 then的內部不能使用Vue的實例化的this, 因爲在內部 this 沒有被綁定。

 

可以使用ES6的箭頭函數

     axios.get('/api/ServerInfo/GetQueryTypedTSSST'
    ).then((res)=>{
      this.options=res.data
    }).catch(function (error) {
        console.log(error);
    });

 

或者在axios外面定義that

    var that=this
    axios.get('/api/ServerInfo/GetQueryTypedTSSST'
    ).then(function(res){

        this.options=res.data
       
    }).catch(function (error) {
        console.log(error);
    });

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章