Skip to content

toRef

Category
Export Size
159 B
Last Changed
15 minutes ago
Alias
resolveRef
Related

将值、ref 或 getter 规范化为 refcomputed

用法

ts
import { toRef } from '@vueuse/core'

const foo = ref('hi')

const a = toRef(0) // Ref<number>
const b = toRef(foo) // Ref<string>
const c = toRef(() => 'hi') // ComputedRef<string>

类型声明

typescript
/**
 * 将值、ref 或 getter 规范化为 `ref` 或 `computed`。
 */
export declare function toRef<T>(r: () => T): Readonly<Ref<T>>
export declare function toRef<T>(r: ComputedRef<T>): ComputedRef<T>
export declare function toRef<T>(r: MaybeRefOrGetter<T>): Ref<T>
export declare function toRef<T>(r: T): Ref<T>
export declare function toRef<T extends object, K extends keyof T>(
  object: T,
  key: K,
): ToRef<T[K]>
export declare function toRef<T extends object, K extends keyof T>(
  object: T,
  key: K,
  defaultValue: T[K],
): ToRef<Exclude<T[K], undefined>>
/**
 * @deprecated use `toRef` instead
 */
export declare const resolveRef: typeof toRef

Source

SourceDocs

贡献者

Anthony Fu
丶远方
Anthony Fu

变更日志

v10.0.0 on 4/14/2023
cb6f9 - fix: improve types overload, close #2925
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
f307f - feat: support two argument overrides from Vue
10e98 - feat!: rename resolveRef to toRef

Released under the MIT License.