gpt4 book ai didi

vue.js - 如何动态地将新属性添加到 v-for 中的对象数组 - Vue.js

转载 作者:搜寻专家 更新时间:2023-10-30 22:15:04 25 4
gpt4 key购买 nike

我想向 v-for 中的对象数组动态添加额外的属性 (showMore)。是否可以在 v-for 中执行此操作,例如:

this.students = [
{
name: 'anonymous',
class: 'Sixth',
otherInfo: "..."
},
{
name: 'anonymous2',
class: 'Sixth',
otherInfo: "..."
}
]
<div v-for="student in students">
<div>{{student.name}}</div>
<div>{{student.class}}</div>
<div @click="student.showMore = true">Show more +</div>
<!-- it was possible in angularjs. is it possible in vuejs ?-->
</div>

在这里,我通过单击“显示更多”向对象数组 (student) 添加了一个属性 (showMore)。 v-for里面有可能吗?如果不是那么如何实现这一点(最佳实践)?

最佳答案

当然可以,但它不会 react 。如果你想要,你需要使用:

vm.$set( target, key, value )

https://v2.vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats

关于vue.js - 如何动态地将新属性添加到 v-for 中的对象数组 - Vue.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47052445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com