【发布时间】:2018-02-12 10:52:39
【问题描述】:
我有一个呈现数组初始状态的v-for 列表。但是当我将项目添加到数组时,渲染不会更新。
文档和此处的大多数答案都提到您必须使用 this.$set 而不是 array.push(),但在我的情况下这没有帮助。
当调用addTag("thing") 时,“事物”被添加到数组中并且它在 Vue 检查器中可见。只是v-for 没有更新。
模板
<span v-for="(tag, index) in project.tags" :key="index">
{{tag}} // this renders all the tags that are initially available
</span>
代码(vue typescript类模板)
export default class Student extends Vue {
project:Project = {} as Project
newtag : string = ""
addTag() {
// adds to array, but v-for not updating
this.$set(this.project.tags, this.project.tags.length, this.newtag)
// adds to array, but v-for not updating
this.project.tags.push(this.newtag)
}
}
编辑此代码使用typescript class component
【问题讨论】:
-
为什么不把
project放在data里面? -
因为它使用了typescript类模板github.com/vuejs/vue-class-component
-
哇,我不知道这个。谢谢
-
是的,它非常棒,让代码更具可读性
标签: typescript vue.js v-for vue-class-components