主题
computedEager
预计算而无需惰性求值。
🌐 Eager computed without lazy evaluation.
INFO
此函数将在未来版本中被移除。
TIP
注意💡:如果你使用的是 Vue 3.4 及以上版本,你可以直接使用 computed,不再需要这个函数。 在 Vue 3.4 及以上版本中,如果计算的新值没有变化,computed、effect、watch、watchEffect、render 依赖将不会被触发。 参考:https://github.com/vuejs/core/pull/5912
在此了解更多:Vue:何时计算属性可能不是正确的工具。
🌐 Learn more at Vue: When a computed property can be the wrong tool.
- 当你进行复杂计算时使用
computed(),这种计算实际上可以从缓存和惰性求值中受益,并且只有在真正必要时才应(重新)计算。 - 当操作简单且返回值很少变化时(通常是布尔值),使用
computedEager()。
示例
用法
🌐 Usage
ts
import { computedEager } from '@vueuse/core'
const todos = ref([])
const hasOpenTodos = computedEager(() => !!todos.length)
console.log(hasOpenTodos.value) // false
toTodos.value.push({ title: 'Learn Vue' })
console.log(hasOpenTodos.value) // true