跳到主要内容

computedEager

分类
导出大小
189 B
上次更改
上个月
别名
eagerComputed

没有惰性求值的急切计算。

信息

此函数将在未来版本中移除。

提示

注意💡:如果您使用的是 Vue 3.4+,您可以直接使用 computed,不再需要此函数。在 Vue 3.4+ 中,如果计算出的新值没有改变,computedeffectwatchwatchEffectrender 依赖项将不会被触发。请参阅:https://github.com/vuejs/core/pull/5912

了解更多信息,请访问Vue: When a computed property can be the wrong tool

  • 当您进行复杂的计算,并且可以从缓存和惰性求值中获益,并且只应在真正必要时(重新)计算时,请使用 computed()
  • 当您进行简单的操作,返回值很少改变(通常是布尔值)时,请使用 computedEager()

演示

用法

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

来源

源代码演示文档

贡献者

Anthony Fu
Anthony Fu
IlyaL
Vida Xie
SerKo
Robin
IlyaL
briwa
Doctorwu
Jonathan Tovar Diaz
wheat

更新日志

26a7d - feat: 废弃 computedEager (#5034)
e5f74 - feat!: 弃用别名导出,转而使用原始函数名称 (#5009)
v13.1.0
c1d6e - feat(shared): 确保返回类型存在 (#4659)
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v10.7.2
b6d8f - fix: 适应 vue3.4+ 的变化 (#3689)