Vue v-for Components
Using v-for, components can be combined to create a large number of identical pieces.
Additionally useful when producing elements with v-for from a component is the ability to dynamically assign props based on values from an array.
Create Component Elements with v-for
Now, using v-for, we will generate component elements based on an array containing the names of food items.
Example
App.vue:
Food
Components created with v-for based on an array.
FoodItem.vue:
{{ foodName }}
v-bind Shorthand
We utilize v-bind to dynamically bind props, and since we will be using v-bind a lot more now than in the past, we will use the v-bind: shorthand : for the remainder of this lesson.
The 'key' Attribute
Because of the way Vue updates elements produced using v-for, issues may arise if we edit the array after the elements have been formed. Since Vue reuses elements to maximize efficiency, removing an item may result in the reuse of previously created elements rather than the creation of all new ones, potentially changing element characteristics.
Since elements lack a unique identifier, they are wrongly reused. To prevent this, we utilize the key attribute, which allows Vue to distinguish between the elements.
First, let’s create a web page containing foods using v-for to display the food name, description, favorite food image, and button to alter the favorite status. Then, we will develop erroneous behavior without the key property.
App.vue:
Food
Food items are generated with v-for from the 'foods' array.
FoodItem.vue:
{{ foodName }}
{{ foodDesc }}
Let’s make a button that eliminates the second entry in the array to demonstrate the requirement for the key attribute. This is incorrect because the favorite picture is moved from the “Fish” element to the “Cake” element without the key attribute:
Example
The addition of a button is the sole modification from the preceding example:
and a method:
methods: {
removeItem() {
this.foods.splice(1,1);
}
}
in App.vue.
As previously noted, Vue optimizes the page by reusing elements, but at the same time, Vue is unable to properly distinguish between the elements. This is why the favorite picture switches from “fish” to “cake” when an element is removed. For this reason, whenever we generate items using v-for, we should always add the key attribute to uniquely mark each element. This issue is resolved when the key attribute is used.
Since the array element index varies when array elements are added and removed, we do not utilize it as the key attribute value. The food products already have unique names, so we can use that instead of creating a new data field to maintain a unique value for each item, like an ID number:
Example
To solve the issue and uniquely identify every element made with v-for, we only need to add one line to App.vue: