美文网首页
浅析--Vue之计算属性 vs Methods

浅析--Vue之计算属性 vs Methods

作者: Just_0818 | 来源:发表于2017-08-13 10:13 被阅读0次

    相信了解Vue的人都知道计算属性computed,在表面上看来computed和methods都能达到同样的效果,比如:

    methods: {
          reversedMessage: function () {
            return this.message.split('').reverse().join('')
          }
        },
        computed: {
          reversedMessage: function () {
            // `this` points to the vm instance
            return this.message.split('').reverse().join('')
          }
        }
    

    但是存在即合理,computed的特性就是可以把计算的值缓存,只有在相关依赖发生改变时才会重新求值,而methods每次都会重新求值,所以在遇到大量计算或者遍历一个极大的数组时,前者的好处就不言而喻了。

    相关文章

      网友评论

          本文标题:浅析--Vue之计算属性 vs Methods

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