From a107776914f85563efbf1fe47a4ef214ac1c0c4d Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 6 Jul 2022 18:17:44 +0800 Subject: [PATCH] fix(computedWithControl): `trigger` only work in Vue 3 --- packages/shared/computedWithControl/index.md | 2 ++ packages/shared/computedWithControl/index.test.ts | 4 ++-- packages/shared/computedWithControl/index.ts | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/shared/computedWithControl/index.md b/packages/shared/computedWithControl/index.md index 1af150c1736..852ddbd41f1 100644 --- a/packages/shared/computedWithControl/index.md +++ b/packages/shared/computedWithControl/index.md @@ -37,6 +37,8 @@ console.log(computedRef.value) // 1 ### Manual Triggering +> This only works in Vue 3 + You can also manually trigger the update of the computed by: ```ts diff --git a/packages/shared/computedWithControl/index.test.ts b/packages/shared/computedWithControl/index.test.ts index b0aaf86a8d5..634787b24bf 100644 --- a/packages/shared/computedWithControl/index.test.ts +++ b/packages/shared/computedWithControl/index.test.ts @@ -1,4 +1,4 @@ -import { ref } from 'vue-demi' +import { isVue3, ref } from 'vue-demi' import { computedWithControl } from '.' describe('computedWithControl', () => { @@ -19,7 +19,7 @@ describe('computedWithControl', () => { expect(computed.value).toBe('BAR') }) - it('custom trigger', () => { + it.runIf(isVue3)('custom trigger', () => { let count = 0 const computed = computedWithControl(() => {}, () => count) diff --git a/packages/shared/computedWithControl/index.ts b/packages/shared/computedWithControl/index.ts index afa875421ad..257b55e7053 100644 --- a/packages/shared/computedWithControl/index.ts +++ b/packages/shared/computedWithControl/index.ts @@ -67,7 +67,9 @@ export function computedWithControl( } }) as ComputedRefWithControl - result.trigger = update + if (Object.isExtensible(result)) + result.trigger = update + return result }