我需要為 v-for 元素設定一個偽/自動生成的 :key,因為專案的 id 可能是重復的。
我試過這個:
<my-component v-for="items in item" v-bind:key="getPseudoIds()">
...
props:{
'id' : {
default: 0,
type: Number,
}
...
getPseudeIds(){
return this.id ;
},
但我總是得到一個
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "id"
如果我將 id 宣告為資料,我會得到一個infinite loop error
uj5u.com熱心網友回復:
關鍵是相關的嗎?您可以使用索引作為鍵
<my-component v-for="(item, index) in items" :key="index">
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/426730.html
標籤:Vue.js
