Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue 'key' 属性


示例

key 属性用于 food-item 组件,以唯一标识使用 v-for 创建的每个元素。

<food-item
  v-for="x in foods"
  :key="x.name"
  :food-name="x.name"
  :food-desc="x.desc"
  :is-favorite="x.favorite"
/>
运行示例 »

定义和使用

key 属性与 v-for 指令一起使用,以便 Vue 可以正确区分元素。

Vue 通过重用元素来优化性能。因此,当使用 v-for 从数组创建元素时,如果未使用 key 属性,则在数组修改时,元素属性可能会混合在一起。

有关更详细的解释以及未使用 key 属性时出现错误的示例,请参阅 Vue 教程中的 'key' 属性 章。


相关页面

Vue 教程: Vue v-for 指令

Vue 教程: Vue v-for 组件

Vue 教程: Vue 使用 v-for 的动画


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.