英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论