Vuejs2 使用v-for时,为什么键属性值不会传播到子组件?
为什么子组件中的Vuejs2 使用v-for时,为什么键属性值不会传播到子组件?,vuejs2,Vuejs2,为什么子组件中的键道具值为空 Vue代码: Vue.component('my-column',{ props: ['items'], template: ` <div> <my-row v-for="(item, index) in items" :item="item" :index="index" :key="item.id" ></my-row> </div&
键
道具值为空
Vue代码:
Vue.component('my-column',{
props: ['items'],
template: `
<div>
<my-row
v-for="(item, index) in items"
:item="item"
:index="index"
:key="item.id"
></my-row>
</div>
`,
})
Vue.component('my-row',{
props: ['item', 'index', 'key'],
template: `
<div class="item--row">
<p>{{ item.name }} {{ item.id }} - {{ key }}</p>
</div>
`
})
new Vue({
el: '#app',
data: function () {
return {
items: [
{ id: 0, name: 'one' },
{ id: 1, name: 'two' },
{ id: 2, name: 'three' },
]
}
},
})
预期产出:
one 0 - 0
two 1 - 1
three 2 - 2
这很可能是因为密钥已注册。您必须将密钥重命名为其他对象才能将其用作道具<代码>键属性用于告诉Vue跟踪元素以进行优化 如果您不提供密钥,Vue将尽最大努力重用看起来相似的代码。这有时可能不是你想要的。我相信这就是为什么要引入
键的原因
one 0 -
two 1 -
three 2 -
one 0 - 0
two 1 - 1
three 2 - 2