Skip to content

extendRef

向 Ref 添加额外属性。

¥Add extra attributes to Ref.

警告

该函数仅适用于 Vue 2.7 或以上版本。

¥This function only works for Vue 2.7 or above.

用法

¥Usage

请注意,额外的属性在 Vue 的模板中将不可访问。

¥Please note the extra attribute will not be accessible in Vue's template.

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 将被展开并做出反应

¥Refs will be unwrapped and be reactive

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' // will trigger update
extraRef.value === 'new data'

类型声明

typescript
export interface ExtendRefOptions<Unwrap extends boolean = boolean> {
  /**
   * Is the extends properties enumerable
   *
   * @default false
   */
  enumerable?: boolean
  /**
   * Unwrap for Ref properties
   *
   * @default true
   */
  unwrap?: Unwrap
}
/**
 * Overload 1: Unwrap set to 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
/**
 * Overload 2: Unwrap unset or set to true
 */
export declare function extendRef<
  R extends Ref<any>,
  Extend extends object,
  Options extends ExtendRefOptions,
>(ref: R, extend: Extend, options?: Options): Extend & R

源代码

源代码文档

变更日志

No recent changes

VueUse 中文网 - 粤ICP备13048890号