最近在学习vue,使用axios+v-for做个搜索天气的小练习,发现只有第一次搜索有数据,改变数组后vue-for不渲染,但使用console.log可以看到数据确实更新了。
使用this.$forceUpdate();
这个不起作用,没办法了,只好在js里面写了一个清空数组的办法,解决了问题。
var app = new Vue({
el: "#app",
data: {
list: [],
city: ""
},
methods: {
setWeather: function () {
var that = this;
//每执行此方法,提前清空数组,保证往下执行代码,数组为空
this.list = [];
axios.get("http://wthrcdn.etouch.cn/weather_mini?city=" + this.city).then(
function (response) {
console.log(response.data.data.forecast);
that.list = response.data.data.forecast;
}
).catch(function (err) {
console.log(err)
});
}
}
})