Vue 3中的`reactive`对象的getter会转化为计算属性吗?

huangapple go评论68阅读模式
英文:

Are Vue 3 `reactive` object getters made into computed values?

问题

在这个例子中,bookCount 是否和computed引用一样被记忆了呢?

const author = reactive({
  name: 'John Doe',
  books: [
    'Vue 2 - Advanced Guide',
    'Vue 3 - Basic Guide',
    'Vue 4 - The Mystery'
  ],

  get bookCount() {
    return this.books.length
  }
})
英文:

In this example, is bookCount memoized the same way as a computed ref is?

const author = reactive({
  name: 'John Doe',
  books: [
    'Vue 2 - Advanced Guide',
    'Vue 3 - Basic Guide',
    'Vue 4 - The Mystery'
  ],

  get bookCount() {
    return this.books.length
  }
})

答案1

得分: 2

不是的。每次访问属性时都会执行getter。

您可以通过在模板中使用getter并触发模板重新渲染来进行检查。您会看到getter会在每次渲染时执行,而普通的computed不会(只要值没有更改)。

sfc.vuejs.org上查看此示例。

英文:

No, it is not. The getter will be executed everytime the property is accessed.

You can check it by using the getter in the template, and provoke a template re-render. You'll see the getter will be executed at every render, while a regular computed won't (as long as the value didn't change).

Check out this example on sfc.vuejs.org

huangapple
  • 本文由 发表于 2023年2月6日 13:41:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定