zoukankan      html  css  js  c++  java
  • 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);
        });
  • 相关阅读:
    22 有序化模块
    21模块
    Day20 继承
    Day19 约束
    面向对象 成员
    面向对象01
    内置函数、匿名函数、递归、二分法
    生成器函数 推导式
    Unity3D 实现方块跑酷
    day30-2018-12-3-进程
  • 原文地址:https://www.cnblogs.com/JinweiChang/p/12719450.html
Copyright © 2011-2022 走看看