Skip to content

toRefs

类别
导出体积
285 B
上次更改
5 months ago

扩展了 toRefs,还接受对象引用的 refs。

用法

ts
import { 
toRefs
} from '@vueuse/core'
import {
reactive
,
ref
} from 'vue'
const
objRef
=
ref
({
a
: 'a',
b
: 0 })
const
arrRef
=
ref
(['a', 0])
const {
a
,
b
} =
toRefs
(
objRef
)
const [
a
,
b
] =
toRefs
(
arrRef
)
const
obj
=
reactive
({
a
: 'a',
b
: 0 })
const
arr
=
reactive
(['a', 0])
const {
a
,
b
} =
toRefs
(
obj
)
const [
a
,
b
] =
toRefs
(
arr
)

使用场景

解构 props 对象

vue
<script lang="ts">
import { 
toRefs
,
useVModel
} from '@vueuse/core'
export default {
setup
(
props
) {
const
refs
=
toRefs
(
useVModel
(
props
, 'data'))
console
.
log
(
refs
.a.value) // props.data.a
refs
.a.value = 'a' // emit('update:data', { ...props.data, a: 'a' })
return { ...
refs
}
} } </script> <template> <
div
>
<
input
v-model
="a"
type
="text">
<
input
v-model
="b"
type
="text">
</
div
>
</template>

类型声明

ts
export interface ToRefsOptions {
  /**
   * 当属性更新时,是否用副本替换原始的 ref。
   *
   * @default true
   */
  
replaceRef
?:
MaybeRefOrGetter
<boolean>
} /** * 扩展的 `toRefs`,还接受对象的 ref。 * * @see https://vueuse.org/toRefs * @param objectRef 一个 ref 或普通对象或数组。 * @param options Options */ export declare function
toRefs
<
T
extends object>(
objectRef
:
MaybeRef
<
T
>,
options
?: ToRefsOptions,
):
ToRefs
<
T
>

源码

源码文档

贡献者

Anthony Fu
一纸忘忧
Anthony Fu
丶远方
Robin
IlyaL
Aaron-zon
Kasper Seweryn
ByMykel
vaakian X
chaii3
webfansplz
William T. Kirby

更新日志

c1d6e - feat(shared): ensure return types exists (#4659)
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
5309c - fix: don't trigger unwanted watchEffects (#3260)
9f998 - fix: fix stack overflow caused by recursive setPrototypeOf (#3166)