我在 v-for 循环中渲染子组件,但一次只显示一个。我假设 Vue 将为每个渲染创建一个单独的/新的子组件。
但它似乎使用了相同的实例,因为我在子组件中的数据值即使在被更改后仍然存在。
现在,我正在创建一个观察者来在问题发生变化时手动重置数据值......但这感觉很糟糕。
这是预期的行为吗?
// Parent Component
<AnswerQuestion
v-for = "question in questions"
v-bind:question = "question"
v-if = "question.id == currentVisibleIndex"
v-on:questionAnswered = "questionAnswered"
>
</AnswerQuestion>
// Child Component
props: ["question"],
data() {
return {
options: options,
commentText: null
}
}
// what i am doing now, feels hacky
watch: {
question: function (newQuestion) {
this.selectedOption = null
this.commentText = null
}
},
Run Code Online (Sandbox Code Playgroud)
是的,重用组件和元素是可取的,这对性能更好。
要强制 Vue 创建新实例,请为该key属性使用唯一值,例如问题 ID:
v-bind:key="question.id"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1905 次 |
| 最近记录: |