TypeError: Cannot set property 'options' of undefined

Posted jinweichang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
    });

 

以上是关于TypeError: Cannot set property 'options' of undefined的主要内容,如果未能解决你的问题,请参考以下文章