Skip to content

Commit

Permalink
feat(extendRef): support 2.7, close #2340
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 19, 2022
1 parent 81f626f commit 8c82e5e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/shared/extendRef/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add extra attributes to Ref.

## Usage

> This function is **NOT compatible with Vue 2**.
> This function is **NOT compatible with Vue 2.6 or below**.
> Please note the extra attribute will not be accessible in Vue's template.
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/extendRef/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Ref, ShallowUnwrapRef } from 'vue-demi'
import { isRef } from 'vue-demi'
import { __onlyVue3 } from '../utils/compatibility'
import { __onlyVue27Plus } from '../utils/compatibility'

export interface ExtendRefOptions<Unwrap extends boolean = boolean> {
/**
Expand Down Expand Up @@ -29,7 +29,7 @@ export function extendRef<R extends Ref<any>, Extend extends object, Options ext

// implementation
export function extendRef<R extends Ref<any>, Extend extends object>(ref: R, extend: Extend, { enumerable = false, unwrap = true }: ExtendRefOptions = {}) {
__onlyVue3()
__onlyVue27Plus()

for (const [key, value] of Object.entries(extend)) {
if (key === 'value')
Expand Down
9 changes: 8 additions & 1 deletion packages/shared/utils/compatibility.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isVue3 } from 'vue-demi'
import { isVue3, version } from 'vue-demi'

export function __onlyVue3(name = 'this function') {
if (isVue3)
Expand All @@ -7,6 +7,13 @@ export function __onlyVue3(name = 'this function') {
throw new Error(`[VueUse] ${name} is only works on Vue 3.`)
}

export function __onlyVue27Plus(name = 'this function') {
if (isVue3 || version.startsWith('2.7.'))
return

throw new Error(`[VueUse] ${name} is only works on Vue 2.7 or above.`)
}

export const directiveHooks = {
mounted: (isVue3 ? 'mounted' : 'inserted') as 'mounted',
updated: (isVue3 ? 'updated' : 'componentUpdated') as 'updated',
Expand Down

0 comments on commit 8c82e5e

Please sign in to comment.