尝试使用Vue js发送http get请求。逻辑上没有发现任何问题,但我对vuejs的使用经验不是很丰富。
一直遇到这两个错误:
[Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'get' of undefined"
和
TypeError: Cannot read property 'get' of undefined.
一直遇到这两个错误:
[Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'get' of undefined"
和
TypeError: Cannot read property 'get' of undefined.
var owm = new Vue({
el: '#owm',
data: {
debug: true,
weather: []
},
methods: {
loadWeather: function() {
this.$http.get('http://api.openweathermap.org/data/2.5/find?q=stockholm&type=like&appid=766b78c39446a8fa6313c3b7b2063ade', function(data, status, request){
if(status == 200)
{
this.weather = data;
console.log(this.weather);
}
});
}
},
mounted: function () {
this.loadWeather();
}
});
使用vue resource更新了代码,错误已经消除,但是无法记录任何数据,可能出了什么问题?
Vue.use(VueResource);
var owm = new Vue({
el: '#owm',
data: {
weather: []
},
methods: {
loadWeather: function() {
this.$http.get('http://api.openweathermap.org/data/2.5/find?q=stockholm&type=like&appid=[API KEY]', function(data, status, request){
if(status == 200)
{
this.weather = data;
console.log(this.weather);
}
});
}
},
mounted: function () {
this.loadWeather();
}
});
编辑: 这段代码可用,虽然不太理解 .then 函数以及为什么请求不能使用回调函数,但是可以使用 .then 函数。
this.$http.get('http://api.openweathermap.org/data/2.5/find?q=stockholm&type=like&appid=[API KEY]').then((data) => {
this.weather = data;
console.log(this.weather);
vue-resorce
包。它现在已经被弃用了。阅读有关替代方案的内容:https://medium.com/the-vue-point/retiring-vue-resource-871a82880af4 - user6748331