computedEager
急切计算,没有惰性求值。
注意💡:如果你正在使用 Vue 3.4+,你可以直接使用
computed。在 Vue 3.4+ 中,如果计算属性的新值没有改变,computed、effect、watch、watchEffect、render 的依赖将不会被触发。参见:https://github.com/vuejs/core/pull/5912在 Vue:何时计算属性可能是错误的工具 中了解更多。
- 当你有一个复杂的计算正在进行,并且可以从缓存和惰性求值中实际获益,并且只有在真正必要时才应该(重新)计算时,请使用
computed()。 - 当你有一个简单的操作,并且返回值很少改变(通常是布尔值)时,请使用
computedEager()。
使用
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
类型声明
export type ComputedEagerOptions = WatchOptionsBase
export type ComputedEagerReturn<T = any> = Readonly<ShallowRef<T>>
/**
* Note: If you are using Vue 3.4+, you can straight use computed instead.
* Because in Vue 3.4+, if computed new value does not change,
* computed, effect, watch, watchEffect, render dependencies will not be triggered.
* refer: https://github.com/vuejs/core/pull/5912
*
* @param fn effect function
* @param options WatchOptionsBase
* @returns readonly shallowRef
*/
export declare function computedEager<T>(
fn: () => T,
options?: ComputedEagerOptions,
): ComputedEagerReturn<T>
export { computedEager as eagerComputed }