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);
    });

 

posted @ 2020-04-17 13:47  JinweiChang  阅读(524)  评论(0编辑  收藏  举报