Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vuejs2 使用v-for时,为什么键属性值不会传播到子组件?_Vuejs2 - Fatal编程技术网

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