网页 综合 更新时间 2026-11-13 16:57:43

Reactivity API: Utilities | Vue.js

自动蜘蛛 vuejs.org 正文约 3050 字
Reactivity API: Utilities | Vue.js
Reactivity: Utilities
Reactivity: Advanced
Dependency Injection
Options: Composition
Syntax Specification
Server-Side Rendering
TypeScript Utility Types
Checks if a value is a ref object.
Note the return type is a type predicate, which means isRef can be used as a type guard:
Returns the inner value if the argument is a ref, otherwise return the argument itself. This is a sugar function for val = isRef(val) ? val.value : val.
Can be used to normalize values / refs / getters into refs (3.3+).
Can also be used to create a ref for a property on a source reactive object. The created ref is synced with its source property: mutating the source property will update the ref, and vice-versa.
Normalization signature (3.3+):
Object property signature:
Note this is different from:
The above ref is not synced with state.foo, because the ref() receives a plain number value.
toRef() is useful when you want to pass the ref of a prop to a composable function:
When toRef is used with component props, the usual restrictions around mutating the props still apply. Attempting to assign a new value to the ref is equivalent to trying to modify the prop directly and is not allowed. In that scenario you may want to consider using computed with get and set instead. See the guide to using v-model with components for more information.
When using the object property signature, toRef() will return a usable ref even if the source property doesn't currently exist. This makes it possible to work with optional properties, which wouldn't be picked up by toRefs.
Normalizes values / refs / getters to values. This is similar to unref(), except that it also normalizes getters. If the argument is a getter, it will be invoked and its return value will be returned.
This can be used in Composables to normalize an argument that can be either a value, a ref, or a getter.
Normalizing arguments in composables:
Converts a reactive object to a plain object where each property of the resulting object is a ref pointing to the corresponding property of the original object. Each individual ref is created using toRef().
toRefs is useful when returning a reactive object from a composable function so that the consuming component can destructure/spread the returned object without losing reactivity:
toRefs will only generate refs for properties that are enumerable on the source object at call time. To create a ref for a property that may not exist yet, use toRef instead.
Checks if an object is a proxy created by reactive(), readonly(), shallowReactive() or shallowReadonly().
Checks if an object is a proxy created by reactive() or shallowReactive().
Checks whether the passed value is a readonly object. The properties of a readonly object can change, but they can't be assigned directly via the passed object.
The proxies created by readonly() and shallowReadonly() are both considered readonly, as is a computed() ref without a set function.
Checks if an object is a proxy created by shallowRef, shallowReactive() or shallowReadonly().
Edit this page on GitHub
访问原文 返回搜索 https://vuejs.org/api/reactivity-utilities
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。