⊗jsvuPmLpCn 33 of 72 menu

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:

data() { return { arr: [1, 2, 3, 4, 5], } }

Let's iterate over this array using a loop:

<template> <ul> <li v-for="elem in arr"> {{ elem }} </li> </ul> </template>

Now let's impose a condition on the elements to be shown:

<template> <ul> <template v-for="elem in arr"> <li v-if="elem % 2 === 0"> {{ elem }} </li> </template> </ul> </template>

The following array is given:

data() { return { items: [1, -2, 3, -4, 5], } }

Loop through this array and print only the positive elements of the array.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline