From: Evan You Date: Thu, 6 May 2021 19:45:42 +0000 (-0400) Subject: wip: defineReactive on instance with keys starting with $ X-Git-Tag: v3.1.0-beta.1~29 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=5a0bab0bd262639d3a5a208764005f6fd5e8e522;p=thirdparty%2Fvuejs%2Fcore.git wip: defineReactive on instance with keys starting with $ --- diff --git a/packages/runtime-core/src/compat/global.ts b/packages/runtime-core/src/compat/global.ts index 9f353d414d..2466706650 100644 --- a/packages/runtime-core/src/compat/global.ts +++ b/packages/runtime-core/src/compat/global.ts @@ -11,7 +11,6 @@ import { isFunction, extend, NOOP, - EMPTY_OBJ, isArray, isObject, isString, @@ -557,11 +556,8 @@ function defineReactive(obj: any, key: string, val: any) { const i = obj.$ if (i && obj === i.proxy) { - // Vue instance, add it to data - if (i.data === EMPTY_OBJ) { - i.data = reactive({}) - } - i.data[key] = val + // target is a Vue instance - define on instance.ctx + defineReactiveSimple(i.ctx, key, val) i.accessCache = Object.create(null) } else if (isReactive(obj)) { obj[key] = val diff --git a/packages/vue-compat/__tests__/global.spec.ts b/packages/vue-compat/__tests__/global.spec.ts index 3cd2c9a18d..02d578772b 100644 --- a/packages/vue-compat/__tests__/global.spec.ts +++ b/packages/vue-compat/__tests__/global.spec.ts @@ -360,6 +360,20 @@ describe('GLOBAL_PRIVATE_UTIL', () => { expect(vm.$el.textContent).toBe('2') }) + test('defineReactive on instance with key that starts with $', async () => { + const vm = new Vue({ + beforeCreate() { + // @ts-ignore + Vue.util.defineReactive(this, '$foo', 1) + }, + template: `
{{ $foo }}
` + }).$mount() as any + expect(vm.$el.textContent).toBe('1') + vm.$foo = 2 + await nextTick() + expect(vm.$el.textContent).toBe('2') + }) + test('defineReactive with object value', () => { const obj: any = {} const val = { a: 1 }