Skip to content

toRef

类别
导出体积
180 B
上次更改
8 minutes ago
别名
resolveRef

将值、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>

Differences from Vue's toRef

VueUse's toRef is not the same as Vue’s toRef from the vue package.

VueUse toRef

  • Accepts value, ref, or getter
  • Returns:
    • a ref for primitive values
    • a ref for existing refs
    • a computed for getter functions
  • Does not accept object + key
  • Getters always produce readonly computed values

Vue toRef

  • Accepts only:
    • a reactive object + property key, or
    • an existing ref
  • Produces a writable ref linked to the underlying reactive object
  • Does not accept primitive values
  • Does not accept getter functions

Summary

BehaviorVueUse toRefVue toRef
Accepts primitive values✔️
Accepts getter✔️ (computed)
Accepts existing ref✔️✔️
Accepts object + key✔️
Writable✔️ (except getter)✔️
PurposeNormalize to ref/computedBind to reactive object

类型声明

ts
/**
 * 将值、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>>

源码

源码文档

贡献者

一纸忘忧

更新日志

没有最近的更新日志