Vue

vue/no-v-if-with-v-for

Never put v-if and v-for on the same element; filter in a computed list or move v-if to a wrapper.

moderatestaticfe-vue

Why

In Vue 3, v-if is evaluated before v-for on the same element, so the condition cannot see the loop variable: v-for="user in users" v-if="user.active" throws or hides the whole list. In Vue 2 the order was the reverse, which is why old code and old answers still show this pattern.

Fix

Filter in a computed property and loop over it (v-for="user in activeUsers"). To hide the whole list, put v-if on a wrapper element or a <template> around the loop.

Examples

<!-- bad -->
<li v-for="user in users" v-if="user.active" :key="user.id">{{ user.name }}</li>
<!-- good -->
<li v-for="user in activeUsers" :key="user.id">{{ user.name }}</li>

Sources

Facts

Id
vue/no-v-if-with-v-for
Severity
moderate
Check
static checked in the source by audit
Applies to
vue
Only in projects with
vue nuxt
Taught by the skill
fe-vue Vue components
Since version
0.1.0

Rule file on GitHub

Read it in the terminal

npx github:dawidolko/frontend-skills explain vue/no-v-if-with-v-for