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