Ok, so I should use it but how? First, the key attribute should always be a unique value for each item in the array being iterated over. This is encouraged by the official ES Lint Plugin for Vue that includes a vue/required-v-for-key rule and will probably save you some headaches in the long run. Note that all the examples below assume each item in the array is an object, unless otherwise stated.įirst and foremost my best piece of advice is this: just provide it as much as humanly possible.Throughout my experience with Vue I've seen some misunderstanding around the key attribute (as well as had plenty of misunderstanding of it on my own) and so I want to provide some tips on how to and how NOT to use it. Thus it does not have to create any new DOM elements or move any DOM elements if it doesn't have to. The purpose of this key attribute is to give "a hint for Vue's virtual DOM algorithm to identify VNodes when diffing the new list of nodes against the old list" ( from Vue.js Docs).Įssentially, it helps Vue identify what's changed and what hasn't. When working with v-for in Vue it is typically recommended to provide a special key attribute.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |