首页 / 知识

Vue3使用 methods 来替代 computed

2023-04-11 16:29:00

Vue3使用 methods 来替代 computed
computed vs methods
 
我们可以使用 methods 来替代 computed,效果上两个都是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值。而使用 methods ,在重新渲染的时候,函数总会重新调用执行。
 
实例
methods: {
  reversedMessage2: function () {
    return this.message.split('').reverse().join('')
  }
}
 
 
可以说使用 computed 性能会更好,但是如果你不希望缓存,你可以使用 methods 属性。

调用函数缓存取值执行依赖

最新内容

相关内容

热门文章

推荐文章

标签云

猜你喜欢