• Home
  • Recent Q&A
  • Java
  • Cloud
  • JavaScript
  • Python
  • SQL
  • PHP
  • HTML
  • C++
  • Data Science
  • DBMS
  • Devops
  • Hadoop
  • Machine Learning
in Vue.JS by
Q:
Why do you need to use key attribute on for directive in VueJS?

1 Answer

0 votes
by

In order to track each node’s identity, and thus reuse and reorder existing elements, you need to provide a unique key attribute for each item with in v-for iteration. An ideal value for key would be the unique id of each item. Let us take an example usage,

<div v-for="item in items" :key="item.id">

  {{item.name}}

</div>

Hence, It is always recommended to provide a key with v-for whenever possible, unless the iterated DOM content is simple. Note: You shouldn’t use non-primitive values like objects and arrays as v-for keys. Use string or numeric values instead.

Related questions

0 votes
asked Feb 4, 2020 in Vue.JS by rajeshsharma
0 votes
asked Oct 21, 2019 in Vue.JS by Tate
...