Skip to content

extendRef

Category
Export Size
282 B
Last Changed
yesterday

为 Ref 添加额外的属性。

使用方法

请注意,在 Vue 的模板中无法访问这些额外的属性。

ts
import { extendRef } from '@vueuse/core'
import { ref } from 'vue'

const myRef = ref('content')

const extended = extendRef(myRef, { foo: 'extra data' })

extended.value === 'content'
extended.foo === 'extra data'

Refs 将被自动解包并保持响应式

ts
const myRef = ref('content')
const extraRef = ref('extra')

const extended = extendRef(myRef, { extra: extraRef })

extended.value === 'content'
extended.extra === 'extra'

extended.extra = 'new data' // 将触发更新
extraRef.value === 'new data'

类型声明

typescript
export interface ExtendRefOptions<Unwrap extends boolean = boolean> {
  /**
   * 扩展属性是否可枚举
   *
   * @default false
   */
  enumerable?: boolean
  /**
   * 是否对 Ref 类型的属性进行解包
   *
   * @default true
   */
  unwrap?: Unwrap
}
/**
 * 重载 1: Unwrap 设置为 false
 */
export declare function extendRef<
  R extends Ref<any>,
  Extend extends object,
  Options extends ExtendRefOptions<false>,
>(ref: R, extend: Extend, options?: Options): ShallowUnwrapRef<Extend> & R
/**
 * 重载 2: Unwrap 未设置或设置为 true
 */
export declare function extendRef<
  R extends Ref<any>,
  Extend extends object,
  Options extends ExtendRefOptions,
>(ref: R, extend: Extend, options?: Options): Extend & R

Source

SourceDocs

贡献者

Anthony Fu
远方os
Anthony Fu
vaakian X

变更日志

v10.0.0-beta.5 on 4/13/2023
13169 - feat(createTemplatePromise): new function (#2957)
v9.8.0 on 12/20/2022
8c82e - feat: support 2.7, close #2340

Released under the MIT License.