【发布时间】:2019-11-26 08:33:45
【问题描述】:
我正在尝试使用从我的服务器 API 获得的值设置我的空对象,这是一个 json。 但是我在同一行一遍又一遍地收到错误:
Uncaught (in promise) TypeError: Cannot set property 'itemListModel' of undefined at eval
我的代码:
data: function() {
return {
itemListModel: {}
}
}
methods: {
getAllItemsFromDb: async () => {
const url = 'https://localhost:44339/ListAll';
await axios.get(url, {
headers: {
'Content-Type': 'application/json'
}}).then((response) => {
this.itemListModel = response.data
})
}
}
computed : {
itemResultsFromDB: function(){
return this.itemListModel
}
}
看了这个之前的问题:Uncaught (in promise) TypeError: Cannot set property of undefined with axios
但我看不出我在做什么不同?
【问题讨论】:
-
在您链接答案的问题中,在调用
axios之前添加let self = this,然后使用变量self在axios.get()中引用它,因为this在某处被重新定义跨度> -
如何调用
getAllItemsFromDb方法?
标签: javascript vue.js axios