详解Vue列表渲染


本文摘自PHP中文网,作者php中世界最好的语言,侵删。

这次给大家带来详解Vue列表渲染详解Vue列表渲染的注意事项有哪些,下面就是实战案例,一起来看一下。

变异方法 (mutation method),顾名思义,会改变被这些方法调用的原始数组。相比之下,也有非变异 (non-mutating method) 方法,例如:filter(), concat() 和 slice() 。这些不会改变原始数组,但总是返回一个新数组。当使用非变异方法时,可以用新数组替换旧数组:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

example1.items = example1.items.filter(function (item) {  return item.message.match(/Foo/)})

<div id="example">

  <div>

    <button @click="concat()">concat</button>

    <button @click="slice()">slice</button>

    <button @click="filter()">filter</button>

  </div>

  <ul>

    <li v-for="item in items">

      {{item.list}}

    </li>

   

  </ul>

</div>

<script>

  var example = new Vue({

    el:"#example",

    data:{

      items:[

        {list:5},

        {list:6},

        {list:7}

       

      ],

      addValue:{list:10}

    },

    methods:{

      concat(){

     this.items= this.items.concat(this.addValue)

      },

      slice(){

      this.items = this.items.slice(1)

      },

      filter(){

      this.items = this.items.filter(function(item,index,arr) {

          return (index > 0)

        })

      }

     

    }

  })

以上操作并不会导致Vue丢弃现有DOM并重新渲染整个列表。Vue实现了一些智能启发式方法来最大化DOM元素重用,所以用一个含有相同元素的数组去替换原来的数组是非常高效的操作

注意事项

由于 JavaScript 的限制,Vue 不能检测以下变动的数组:

当你利用索引直接设置一个项时,例如:

1

vm.items[indexOfItem] = newValue

当你修改数组的长度时,例如:

1

vm.items.length = newLength

为了解决第一类问题,以下两种方式都可以实现和 vm.items[indexOfItem] = newValue 相同的效果,同时也将触发状态更新:

1

2

3

4

// Vue.set

Vue.set(example1.items, indexOfItem, newValue)

// Array.prototype.splice

example1.items.splice(indexOfItem, 1, newValue)

为了解决第二类问题,你可以使用 splice:

1

example1.items.splice(newLength)

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

相关阅读:

在HTML中如何用<a>标签编写个人收藏夹

HTML用img标签做图

HTML里的常见问题一

以上就是详解Vue列表渲染的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

html中的table详解

详解vue列表渲染

h5移动端各种各样的列表的制作方法(一)

html文本格式化的实例详解

indexeddb数据库的使用详解

html中的标签详解

js的图片处理与合成详解

html中的描述列表怎么表示

html的标记文字详解

phonegap创建联系人详解

更多相关阅读请进入《渲染》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...