我正在使用VueJS构建一个气象站应用程序,并试图从OpenWeather API中添加预测数据。我可以成功地提取数据并将其记录到控制台,但由于某种原因,我无法在模板中显示它。
我正在抓取接下来五个小时的数据,并将其存储在数组中。现在我只是想展示第一个小时。这是我的密码:
<template>
<section>
<h1>Forecast</h1>
<i :class="['owf owf-',iconCode[0]]"></i>
<p>Temperature {{ currentTemp[0] }}</p>
</div>
</section>
</template>
<script>
export default {
data: function(){
return {
time: [],
currentTemp: [],
tempMin: [],
desc: [],
iconCode: [],
}
},
methods: {
getWeather() {
var apiKey = "<apikey>";
var location = "<location>";
var units = "metric";
var url = "http://api.openweathermap.org/data/2.5/forecast?id=" + location + "&APPID=" + apiKey + "&units=" + units;
this.$http.get(url).then(response => {
console.log(response.data.list);
for(var i = 0; i < 5; i++){
this.time[i] = response.data.list[i].dt_txt;
this.currentTemp[i] = response.data.list[i].main.temp;
this.tempMin[i] = response.data.list[i].main.temp_min;
this.desc[i] = response.data.list[i].weather[0].description;
this.iconCode[i] = response.data.list[i].weather[0].icon;
}
});
},
},
beforeMount(){
this.getWeather();
},
}
</script>
这成功地获取了我想要的数据并填充了变量(在Vue Dev工具中进行了验证)。模板被成功地注入到页面中,并显示静态文本。但数据没有显示。怎么回事?
编辑:我应该提到,我正在使用Laravel作为后端。我尝试在{{ }前面加上一个@符号,告诉Blade涉及JS,但它只是显示@而不是变量。
发布于 2019-01-24 15:42:43
在getWeather方法中,不能使用arrayi = "value“。这是因为没有跟踪这种物体的变异。
使用array.push(“值”)代替。
https://v2.vuejs.org/v2/guide/reactivity.html
问候
发布于 2019-01-24 17:01:52
如果你想要this.timei,this.currentTempi .
你以前的圈套
for(var a=0 a<5;a++){
this.time[a] = '';
this.currentTemp[a] = '';
.....
}
https://stackoverflow.com/questions/54356002
复制相似问题