vuejs:从阵列重新呈现的所有后续项目中删除项目

发布于 2025-02-08 21:11:15 字数 2240 浏览 2 评论 0 原文

我有一系列在VUE模板中循环循环的对象,每个对象包含< input> s。用户有一个按钮可以在数组中添加其他项目 - 这些都可以很好地效果。 每个新组件都有一个唯一的键

用户可以使用按钮删除任何添加的< s。这在 data.repeats 数组中正确工作 - 删除了该项目。还会更新DOM以删除此项目。

但是,如果我有4个项目并删除第二个项目,则项目3和4将被重新渲染(我可以看到它们已在Vue Dev工具中“更新”)。这将使添加到这些输入(或更改为DOM等)的所有数据。

这是在删除的物品之后才发生的。

根据我的理解,通过在组件VUE上使用:key 应该能够跟踪并重新渲染。

我认为这可能是因为我将项目存储在数组中,而在将一个键删除后,所有键都被一个键放置,所以我尝试使用具有永不更改的对象,但DOM中的结果是相同的 - 删除后的所有项目都将重新渲染。

有没有办法停止此?

<div v-for="(repeat, count) in repeats">
    <template v-for="field in repeat">
        <component
            :is="field.component" 
            :content="field.content"
            :key="field.content.dot_name"
        >
        </component>
    </template>

    <button @click="remove(count)">Remove</button>
</div>

<button @click="add">Add</button>
data() {
    return {
        repeats: []
    }
},

methods: {
    add() {
        this.repeats.push(this.cloneFields());
    },
    remove(key) {
        this.repeats.splice(key, 1);
    },
}

添加的项目:

input> input s(绿色)中添加的三个项目:

“

vue工具显示项目:

删除项目:

中间项目被删除 - 从最后一项(删除按钮显示循环索引):

“

vue工具显示删除的项目和:键在最后一项(...日记。 3 .sub ...) client> client_details.subsidiaries.3.subsidiary_company_name

I have an array of objects which are looped over in a Vue template, each contains <input>s. User’s have a button to add additional items to the array - these are rendered - that all works perfectly. Each new component is given a unique key.

Users are able to remove any of the added <input>s using a button. This works correctly in the data.repeats array - the item is removed. The DOM is also updated to remove this item.

However, if I have 4 items and remove the second, items 3 and 4 will be re-rendered (I can see they were ‘updated’ in in Vue dev tools). This will loose any data added to those inputs (or changes to DOM etc.).

It’s only items after the removed one that this happens to.

From my understanding, by using :key on the component Vue should be able to track it and re-render.

I thought it might be because I was storing the items in an array and when removing one in the centre all the keys after decremented by one so I tried using an object with named properties which never change but the result in the DOM is the same - all items after the removed one get re-rendered.

Is there way to stop this?

<div v-for="(repeat, count) in repeats">
    <template v-for="field in repeat">
        <component
            :is="field.component" 
            :content="field.content"
            :key="field.content.dot_name"
        >
        </component>
    </template>

    <button @click="remove(count)">Remove</button>
</div>

<button @click="add">Add</button>
data() {
    return {
        repeats: []
    }
},

methods: {
    add() {
        this.repeats.push(this.cloneFields());
    },
    remove(key) {
        this.repeats.splice(key, 1);
    },
}

Added items:

Three items added with data in inputs (green):

enter image description here

Vue tools showing items:

enter image description here

.

Remove item:

Middle item removed - and data gone from the last item (the remove button shows the loop index):

enter image description here

Vue tools showing the removed item and :key still the same for last item (...diaries.3.sub...) client_details.subsidiaries.3.subsidiary_company_name:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花ソ最美的依靠 2025-02-15 21:11:15

尝试将键添加到 v-for =“(重复,计数)重复中” 循环

Try adding a key to v-for="(repeat, count) in repeats" loop

我是男神闪亮亮 2025-02-15 21:11:15

根据您的代码计数代表索引,因此对两个循环(例如添加键)(避免使用非键而不是使用字符串或数字)制作不同

<div v-for="(repeat, count) in repeats" :key="'outer' + count">

 <template v-for="(field, index) in repeat" :key="'inner' + index">

As per your code count represent index so make different key for both loops

like add key (avoid a non-primitive key instead of that use string or number) to the outer loop

<div v-for="(repeat, count) in repeats" :key="'outer' + count">

and for the inner loop

 <template v-for="(field, index) in repeat" :key="'inner' + index">
灼痛 2025-02-15 21:11:15
<div v-for="(item,index) in items" :key="item.id">
.....
<button @click="remove(item.id)">Remove</button>

将ID传递给参数

remove(id) {
  const index = this.items.findIndex(item => item.id === id)
  if (index !== -1) {
    this.items.splice(index, 1)
  }
}

您可以尝试以下链接:
https://fontawsonsicons.com/ tryit/code/vue-js-remove-item-from-rom-array-by-id/0

<div v-for="(item,index) in items" :key="item.id">
.....
<button @click="remove(item.id)">Remove</button>

pass the id to parameter

remove(id) {
  const index = this.items.findIndex(item => item.id === id)
  if (index !== -1) {
    this.items.splice(index, 1)
  }
}

you can try this link :
https://fontawesomeicons.com/tryit/code/vue-js-remove-item-from-array-by-id/0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文