v-for
up:: Vue Directives This loops through arrays and objects.
Warning
key
is required when looping through Vue Components.
Getting the index
Looping through objects
(you can also get the index from an object but it is rarely needed)
Looping through numbers
Removing list items
We are passing the index to a function defined in our Vue Methods whenever an item is clicked (using v-on).
When removing an element, Vue moves the dynamic content around.
- You have a list with two elements.
- Remove the first one.
- Vue moves the second into the first one.
This can introduce some bugs.
Best practice
Hence, it is a good idea to add a unique key.