码迷,mamicode.com
首页 > 其他好文 > 详细

TypeError: Cannot set property 'options' of undefined

时间:2020-04-17 14:02:47      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:style   get   UNC   cat   option   The   server   type   class   

 

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

标签:style   get   UNC   cat   option   The   server   type   class   

原文地址:https://www.cnblogs.com/JinweiChang/p/12719450.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!