美文网首页
v-for 中的 key

v-for 中的 key

作者: 围观工程师 | 来源:发表于2018-07-01 15:54 被阅读0次

vue官方文档不推荐使用 index 作为 v-for 的 key,一直不明白为什么,开发中也没有碰到相关问题,今天终于在知乎上碰到了这个问题。

见vvpvvp在问题中的回答

答主提供的测试代码

html

<div id="app">
  <test v-for="(item,index) in list" :key="index">
     <input v-model="item.value" type="input"/>          <button @click="remove(index)">delete</button>
  </test>
  <button @click="add()">add</button>
</div>

js

Vue.component('test', {
  template: '<div><slot></slot><a @click="change">{{a}}</a></div>',
  data: function() {
    return {
      a: "click Me!"
    }
  },
  methods: {
    change: function() {
       this.a = "clicked";
    }
  }
});
new Vue({
  el: '#app',
  data: {
    list: []
  },
  methods: {
    add: function() {
      this.list.push({
        value: 1
      });
    },
    remove: function(index) {
      this.list.splice(index, 1);
    }
  }
})

问题重现

点击add * 3,点击后两个 “click Me!” 使其状态改变,点击第一个 delete

问题出在哪

看了一遍回答中的评论大致明白了原因。由于使用了 index 作为 key ,当删除第一个 test 组件后,第二个 test 组件的 key 由原来的 1 变成了 0 ,第三个 test 组件的 key 由原来的 2 变成了 1 。diff (虚拟DOM的Diff算法,见原问题第一个回答) 发现少了值为 3 的 key ,于是删除其对应的 test 组件,导致了这个问题。

我为什么没触发过这个问题

由于业务原因,我的子组件状态通常取决于父组件data的属性值,即子组件状态不是独立的,需要依赖父组件数据。这使得即使由于 index 导致“删错了”子组件,父组件的数据也会重新同步给子组件,保持子组件状态与父组件数据一致。代码大致如下:

html

<div id="app">
  <test v-for="(item,index) in list" :a="item.a" :key="index" @change="change(index)">
     <input v-model="item.value" type="input"/>          <button @click="remove(index)">delete</button>
  </test>
  <button @click="add()">add</button>
</div>

js

Vue.component('test', {
  template: '<div><slot></slot><a @click="change">{{a}}</a></div>',
  props: ['a'],
  methods: {
    change: function() {
       this.$emit('change')
    }
  }
});
new Vue({
  el: '#app',
  data: {
    list: []
  },
  methods: {
    add: function() {
      this.list.push({
        value: 1,
        a: 'click Me!'
      });
    },
    remove: function(index) {
      this.list.splice(index, 1);
    },
    change (i) {
      this.list[i].a = 'clicked'
    }
  }
})

为什么不推荐使用 index

上面说到,由于 index 导致“删错了”子组件,父组件的数据会重新同步给子组件,保持子组件状态与父组件数据一致。如果使用唯一 id 那么 vue 就不必多做数据同步这一步操作。

相关文章

  • v-for 的四种使用方法

    v-for中key的使用注意项v-for在循环时,key属性只能使用number获取stringkey在使用时只能...

  • v-for 中的 key

    vue官方文档不推荐使用 index 作为 v-for 的 key,一直不明白为什么,开发中也没有碰到相关问题,今...

  • v-for中的key

    大家要知道,不仅只是vue中,react中在执行列表渲染时也会要求给每个组件添加key这个属性如果想知道key的作...

  • 10 vue 遍历

    1 v-for 遍历数组 2 v-for 遍历对象 3, v-for使用过程中添加key 4 哪些数组是响应式的1...

  • v-for 中key的作用

    一句话概括:key标识了每个vnode的唯一性,在vdom改变时能精准找出变化的vnode,减少改变vdom的消耗...

  • v-for 中key的作用

    目前可以理解为遍历数组或元素中的唯一标识,增加或删减元素时,通过这个唯一标识key判断是否是之前的元素,并且该元素...

  • v-for中key的作用

    v-for中key的作用1.当数据发生变化时,vue是怎样更新节点的?渲染真实DOM的开销是很大的,比如有时我们修...

  • vue 报错Elements in iteration expe

    v-for 后添加 :key='item'

  • 为什么使用v-for时必须添加唯一的key?

    首次发表在个人博客 v-for中的key 使用v-for更新已渲染的元素列表时,默认用就地复用策略;列表数据修改的...

  • uni-app运行到H5正常,但运行到小程序不正常、报错总结

    1.v-for中,非 h5 平台 :key 不支持表达式 index+'_' 同级多个 v-for 时 key 的...

网友评论

      本文标题:v-for 中的 key

      本文链接:https://www.haomeiwen.com/subject/xrncuftx.html