Vue

vue/require-v-for-key

Every v-for gives its items a :key from the data; on a template v-for the key goes on the template tag.

umiarkowanastatycznefe-vue

Dlaczego

Without a key, Vue patches list items in place by position. When the list is sorted, filtered or an item is inserted, the DOM nodes and child component state (typed text, open panels, focus) stay where they were and end up next to the wrong data.

Poprawka

Bind a stable, unique value from the item: :key="item.id". On <template v-for>, put the key on the <template> itself (Vue 3), not on its children.

Przykłady

<!-- bad -->
<li v-for="todo in todos">{{ todo.title }}</li>
<!-- good -->
<li v-for="todo in todos" :key="todo.id">{{ todo.title }}</li>

Źródła

Fakty

Identyfikator
vue/require-v-for-key
Waga
umiarkowana
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
vue
Tylko w projektach z
vue nuxt
Uczy jej skill
fe-vue Vue components
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain vue/require-v-for-key