Skip to content

noVueVIfWithVFor

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"nursery": {
"noVueVIfWithVFor": "error"
}
}
}
}

¥Description

禁止在同一元素上使用 v-ifv-for 指令。

¥Disallow using v-if and v-for directives on the same element.

在以下两种常见情况下,这可能会诱使用户这样做:

¥There are two common cases where this can be tempting:

  • 要筛选列表中的项目(例如 v-for="user in users" v-if="user.isActive")。在这种情况下,请将 users 替换为返回已过滤列表的新计算属性(例如 activeUsers)。

    ¥To filter items in a list (e.g. v-for="user in users" v-if="user.isActive"). In these cases, replace users with a new computed property that returns your filtered list (e.g. activeUsers).

  • 避免渲染应隐藏的列表(例如 v-for="user in users" v-if="shouldShowUsers")。在这种情况下,请将 v-if 移至容器元素。

    ¥To avoid rendering a list if it should be hidden (e.g. v-for="user in users" v-if="shouldShowUsers"). In these cases, move the v-if to a container element.

¥Examples

¥Invalid

<TodoItem
v-if="complete"
v-for="todo in todos"
:todo="todo"
/>

¥Valid

<ul v-if="complete">
<TodoItem
v-for="todo in todos"
:todo="todo"
/>
</ul>

¥Related links