computedWithControl
Category
Export Size
250 B
Last Changed
15 minutes ago
Alias
controlledComputed
Related
显式定义计算属性的依赖关系。
用法
ts
import { computedWithControl } from '@vueuse/core'
const source = ref('foo')
const counter = ref(0)
const computedRef = computedWithControl(
() => source.value, // 监视 source,与 `watch` 相同
() => counter.value, // 计算属性的 getter,与 `computed` 相同
)
js
import { computedWithControl } from '@vueuse/core'
const source = ref('foo')
const counter = ref(0)
const computedRef = computedWithControl(
() => source.value, // 监视 source,与 `watch` 相同
() => counter.value,
)
通过这种方式,counter
的更改不会触发 computedRef
更新,但是 source
ref 会。
ts
console.log(computedRef.value) // 0
counter.value += 1
console.log(computedRef.value) // 0
source.value = 'bar'
console.log(computedRef.value) // 1
手动触发
您还可以通过以下方式手动触发计算属性的更新:
ts
const computedRef = computedWithControl(
() => source.value,
() => counter.value,
)
computedRef.trigger()
WARNING
手动触发仅适用于 Vue 3
类型声明
typescript
export interface ComputedWithControlRefExtra {
/**
* 强制更新计算值
*/
trigger: () => void
}
export interface ComputedRefWithControl<T>
extends ComputedRef<T>,
ComputedWithControlRefExtra {}
export interface WritableComputedRefWithControl<T>
extends WritableComputedRef<T>,
ComputedWithControlRefExtra {}
export declare function computedWithControl<T, S>(
source: WatchSource<S> | WatchSource<S>[],
fn: ComputedGetter<T>,
): ComputedRefWithControl<T>
export declare function computedWithControl<T, S>(
source: WatchSource<S> | WatchSource<S>[],
fn: WritableComputedOptions<T>,
): WritableComputedRefWithControl<T>
export { computedWithControl as controlledComputed }
Source
贡献者
Anthony Fu
丶远方
Anthony Fu
Yun Hao
sun0day
kongmoumou
变更日志
v11.0.0-beta.3
on 8/14/2024v10.8.0
on 2/20/2024a086e
- fix: stricter typesv10.0.0-beta.5
on 4/13/2023cb644
- refactor!: remove isFunction
and isString
utils