Conditions in loops in Vue
When looping through elements, you can impose conditions. But the v-for and v-if directives must be written on different tags, otherwise there will be a conflict. To avoid a conflict, the v-for directive is applied to the template tag, and the v-if directive is applied directly to the inserted tag.
Let's look at an example. Let's say we have the following array:
javascriptdata() {
return {
arr: [1, 2, 3, 4, 5],
}
}
Let's iterate over this array using a loop:
html<template>
<ul>
<li v-for="elem in arr">
{{ elem }}
</li>
</ul>
</template>
Now let's impose a condition on the elements to be shown:
html<template>
<ul>
<template v-for="elem in arr">
<li v-if="elem % 2 === 0">
{{ elem }}
</li>
</template>
</ul>
</template>
Задание 1
The following array is given:
javascriptdata() {
return {
items: [1, -2, 3, -4, 5],
}
}
Loop through this array and print only the positive elements of the array.