【发布时间】:2018-08-11 12:58:24
【问题描述】:
我正在尝试理解Vue,它的依赖项(新手)只需要帮助来理解 axios 的工作方式,这样我就可以清楚了。
如果我使用下面的代码,它可以返回我的数据:
methods: {
load: function() {
axios.get('/api/projects')
.then(
response => {
this.projects = (response.data.projects);
}
)
.catch(function (error) {
this.errors.push(error);
console.log(error);
})
}
}
但如果我使用下面的代码,它不会返回数据:
methods: {
load: function() {
axios.get('/api/projects')
.then(function (response) {
this.projects = (response.data.projects);
})
.catch(function (error) {
this.errors.push(error);
console.log(error);
})
}
}
区别仅在于.then 部分。
【问题讨论】:
-
检查您的网络浏览器的调试控制台,我认为您遇到了 CORS 问题developer.mozilla.org/en-US/docs/Web/HTTP/CORS
-
不要调试任何东西,一切都按预期工作。只需谷歌搜索“箭头功能”并在 MDN 上阅读箭头和普通功能之间的区别。并关注
this、context。 -
@jesugmz 谢谢......
-
@VladislavLadicy 谢谢......