diff --git a/packages/core/useVModel/index.test.ts b/packages/core/useVModel/index.test.ts index 013af82ca47..4b510dd28e9 100644 --- a/packages/core/useVModel/index.test.ts +++ b/packages/core/useVModel/index.test.ts @@ -155,4 +155,26 @@ describe('useVModel', () => { expect(dataD.value).toBe(null) expect(dataE.value).toBe('default-data') }) + + it('Should work with classes', async () => { + const emitMock = vitest.fn() + + class SomeClass { + num1 = 1 + + someMethod() {} + } + + const props = { cl: new SomeClass() } + + const ref = useVModel(props, 'cl', emitMock, { passive: true, deep: true }) + + ref.value.num1 = 10 + + await nextTick() + + const emitValue = (emitMock as any).calls[0][1] + + expect(emitValue instanceof SomeClass).toBeTruthy() + }) }) diff --git a/packages/shared/toRefs/index.test.ts b/packages/shared/toRefs/index.test.ts index 1615bcc064a..4b28eb8a3f0 100644 --- a/packages/shared/toRefs/index.test.ts +++ b/packages/shared/toRefs/index.test.ts @@ -96,4 +96,22 @@ describe('toRefs', () => { refs[1].value = 1 expect(spy).toHaveBeenLastCalledWith(['a', 1]) }) + + it('should save instance of class', () => { + class SomeClass { + v = 1 + + fn() { + + } + } + + const obj = ref(new SomeClass()) + + const { v } = toRefs(obj) + + v.value = 10 + + expect(obj.value instanceof SomeClass).toBeTruthy() + }) }) diff --git a/packages/shared/toRefs/index.ts b/packages/shared/toRefs/index.ts index 3925ac7cdb2..25b943c90ba 100644 --- a/packages/shared/toRefs/index.ts +++ b/packages/shared/toRefs/index.ts @@ -31,7 +31,11 @@ export function toRefs( objectRef.value = copy } else { - objectRef.value = { ...objectRef.value, [key]: v } + const newObject = { ...objectRef.value, [key]: v } + + Object.setPrototypeOf(newObject, objectRef.value) + + objectRef.value = newObject } }, }))