computedEager
没有惰性求值的急切计算。
信息
此函数将在未来版本中移除。
提示
注意💡:如果您使用的是 Vue 3.4+,您可以直接使用 computed,不再需要此函数。在 Vue 3.4+ 中,如果计算出的新值没有改变,computed、effect、watch、watchEffect、render 依赖项将不会被触发。请参阅:https://github.com/vuejs/core/pull/5912
了解更多信息,请访问Vue: When a computed property can be the wrong tool。
- 当您进行复杂的计算,并且可以从缓存和惰性求值中获益,并且只应在真正必要时(重新)计算时,请使用
computed()。 - 当您进行简单的操作,返回值很少改变(通常是布尔值)时,请使用
computedEager()。
演示
计数:0
用法
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类型声明
ts
export type ComputedEagerOptions = WatchOptionsBase
export type ComputedEagerReturn<T = any> = Readonly<ShallowRef<T>>
/**
*
* @deprecated This function will be removed in future version.
*
* 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>
/** @deprecated use `computedEager` instead */
export declare const eagerComputed: typeof computedEager