如何在删除列表项时刷新 Vue 组件?
How to refresh Vue components upon list item removal?
我知道如何从 Vue 实例中删除列表项。但是,当列表项传递给Vue组件时,如何在保持组件与列表数据同步的同时删除列表项?
这是用例。考虑一个带有 Markdown 编辑器的在线论坛。我们有一个 Vue 实例,其数据是从服务器获取的已保存评论列表。这些评论应该是用 Markdowns 写的。
为了方便编辑和预览,我们还有一个组件列表。每个组件都包含一个可编辑的输入缓冲区和一个预览部分。 Vue 实例中保存的评论内容用于初始化输入缓冲区并在用户取消编辑时重置它。预览是输入缓冲区内容的转换。
下面是一个测试实现:
<template id="comment">
<div>
Component:
<textarea v-model="input_buffer" v-if="editing"></textarea>
{{ preview }}
<button type="button" v-on:click="edit" v-if="!editing">edit</button>
<button type="button" v-on:click="remove" v-if="!editing">remove</button>
<button type="button" v-on:click="cancel" v-if="editing">cancel</button>
</div>
</template>
<div id="app">
<ol>
<li v-for="(comment, index) in comments">
<div>Instance: {{comment}}</div>
<comment
v-bind:comment="comment"
v-bind:index="index"
v-on:remove="remove">
</comment>
</li>
</ol>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.8/vue.js"></script>
<script>
let comments = ['111', '222', '333']
Vue.component('comment', {
template: '#comment',
props: ['comment', 'index'],
data: function() {
return {
input_buffer: '',
editing: false,
}
},
mounted: function() { this.cancel() },
computed: {
preview: function() {
// This is supposed to be a transformation of the input buffer,
// but for now, let's simply output the input buffer
return this.input_buffer
},
},
methods: {
edit: function() { this.editing = true },
remove: function() { this.$emit('remove', this.index) },
cancel: function() { this.input_buffer = this.comment; this.editing = false },
//save: function() {}, // submit to server; not implemented yet
},
})
let app = new Vue({
el: '#app',
data: { comments: comments },
methods: {
remove: function(index) { this.comments.splice(index, 1); app.$forceUpdate() },
},
})
</script>
问题是,如果我们删除评论,组件不会相应地刷新。比如我们在上面的实现中有3条评论。如果您删除评论 2,项目 3 的预览仍将显示项目 2 的内容。仅当我们按 edit
后跟 cancel
.
时才会更新
我试过 app.$forceUpdate()
,但没有用。
您只需在 v-for 循环中添加 key
属性,如下所示:
<li v-for="(comment, index) in comments" :key="comment">
查看工作 fiddle:https://fiddle.jshell.net/mimani/zLrLvqke/
Vue 尝试通过提供 key 属性来优化渲染,它将那些元素视为完全不同的元素,re-renders 将它们正确对待。
有关详细信息,请参阅 the key
documentation。
试试:
Vue.component('comment', {
template:
`<div>
{{ comment }}
<button v-on:click="remove"> X </button>
</div>`,
props: ['comment', 'index'],
methods: {
remove: function() {
this.$emit('remove', this.index);
}
},
});
vm = new Vue({
el: '#app',
data: {
comments: ['a','b','c','d','e']
},
methods: {
remove: function(index) {
this.comments.splice(index, 1);
},
},
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.8/vue.min.js"></script>
<div id="app">
<ol>
<li v-for="(comment, index) in comments">
<comment
v-bind:comment="comment"
v-bind:index="index"
v-on:remove="remove">
</comment>
</li>
</ol>
</div>
我知道如何从 Vue 实例中删除列表项。但是,当列表项传递给Vue组件时,如何在保持组件与列表数据同步的同时删除列表项?
这是用例。考虑一个带有 Markdown 编辑器的在线论坛。我们有一个 Vue 实例,其数据是从服务器获取的已保存评论列表。这些评论应该是用 Markdowns 写的。
为了方便编辑和预览,我们还有一个组件列表。每个组件都包含一个可编辑的输入缓冲区和一个预览部分。 Vue 实例中保存的评论内容用于初始化输入缓冲区并在用户取消编辑时重置它。预览是输入缓冲区内容的转换。
下面是一个测试实现:
<template id="comment">
<div>
Component:
<textarea v-model="input_buffer" v-if="editing"></textarea>
{{ preview }}
<button type="button" v-on:click="edit" v-if="!editing">edit</button>
<button type="button" v-on:click="remove" v-if="!editing">remove</button>
<button type="button" v-on:click="cancel" v-if="editing">cancel</button>
</div>
</template>
<div id="app">
<ol>
<li v-for="(comment, index) in comments">
<div>Instance: {{comment}}</div>
<comment
v-bind:comment="comment"
v-bind:index="index"
v-on:remove="remove">
</comment>
</li>
</ol>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.8/vue.js"></script>
<script>
let comments = ['111', '222', '333']
Vue.component('comment', {
template: '#comment',
props: ['comment', 'index'],
data: function() {
return {
input_buffer: '',
editing: false,
}
},
mounted: function() { this.cancel() },
computed: {
preview: function() {
// This is supposed to be a transformation of the input buffer,
// but for now, let's simply output the input buffer
return this.input_buffer
},
},
methods: {
edit: function() { this.editing = true },
remove: function() { this.$emit('remove', this.index) },
cancel: function() { this.input_buffer = this.comment; this.editing = false },
//save: function() {}, // submit to server; not implemented yet
},
})
let app = new Vue({
el: '#app',
data: { comments: comments },
methods: {
remove: function(index) { this.comments.splice(index, 1); app.$forceUpdate() },
},
})
</script>
问题是,如果我们删除评论,组件不会相应地刷新。比如我们在上面的实现中有3条评论。如果您删除评论 2,项目 3 的预览仍将显示项目 2 的内容。仅当我们按 edit
后跟 cancel
.
我试过 app.$forceUpdate()
,但没有用。
您只需在 v-for 循环中添加 key
属性,如下所示:
<li v-for="(comment, index) in comments" :key="comment">
查看工作 fiddle:https://fiddle.jshell.net/mimani/zLrLvqke/
Vue 尝试通过提供 key 属性来优化渲染,它将那些元素视为完全不同的元素,re-renders 将它们正确对待。
有关详细信息,请参阅 the key
documentation。
试试:
Vue.component('comment', {
template:
`<div>
{{ comment }}
<button v-on:click="remove"> X </button>
</div>`,
props: ['comment', 'index'],
methods: {
remove: function() {
this.$emit('remove', this.index);
}
},
});
vm = new Vue({
el: '#app',
data: {
comments: ['a','b','c','d','e']
},
methods: {
remove: function(index) {
this.comments.splice(index, 1);
},
},
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.8/vue.min.js"></script>
<div id="app">
<ol>
<li v-for="(comment, index) in comments">
<comment
v-bind:comment="comment"
v-bind:index="index"
v-on:remove="remove">
</comment>
</li>
</ol>
</div>