Skip to content
Advertisement

How to dynamically add/remove slot fields, based on an array in Vue JS

I have the following, which takes a slot containing the HTML fields to be repeated:

JavaScript

When I use removeRow(index), it always removes the last slot. I’ve tested with using the <input v-model="row.value"> and the correct input is removed here, but never the correct slot.

I do not need the inputs in the slot to be dynamic or interact with Vue, I simply want to allow the user to add new rows/remove rows dynamically via a Vue component.

Please see the below two methods I’ve used for adding/removing rows, in case an issue lies here:

JavaScript

Any help would be really appreciated.

Advertisement

Answer

Add a unique key value to your v-for loop element:

JavaScript

This should make sure the correct element is removed from the DOM.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement