当vue页面异步加载的数据想在页面上渲染怎么办
程序员文章站
2024-01-04 16:07:28
处理方法:数组改变用push ......
<template>
<div class="test">
<div v-for="(item, index) in arr" :key="index">
<p>姓名:{{ item.name }}</p>
<p>年龄:{{ item.age }}</p>
<p>爱好:{{ item.like }}</p>
</div>
</div>
</template>
<script>
export default {
data() {
return {
arr: [{ name: "蕾蕾", age: 23 }, { name: "佳佳", age: 20 }]
};
},
created() {
this.arr.foreach(res => {
settimeout(()=>{ //此处模拟异步调数据
res.like = "刷抖音";
})
});
}
}
};
</script>
此时页面是渲染不出来爱好的数据的,如图:
处理方法:数组改变用push
data() {
return {
arr: [{ name: "蕾蕾", age: 23 }, { name: "佳佳", age: 20 }]
};
},
created() {
this.arr.foreach(res => {
this.arr=[]
settimeout(()=>{
res.like = "刷抖音";
this.arr.push(res);
})
});
}