wip: defineReactive on instance with keys starting with $

This commit is contained in:
Evan You 2021-05-06 15:45:42 -04:00
parent 37ee2959fc
commit 5a0bab0bd2
2 changed files with 16 additions and 6 deletions

View File

@ -11,7 +11,6 @@ import {
isFunction, isFunction,
extend, extend,
NOOP, NOOP,
EMPTY_OBJ,
isArray, isArray,
isObject, isObject,
isString, isString,
@ -557,11 +556,8 @@ function defineReactive(obj: any, key: string, val: any) {
const i = obj.$ const i = obj.$
if (i && obj === i.proxy) { if (i && obj === i.proxy) {
// Vue instance, add it to data // target is a Vue instance - define on instance.ctx
if (i.data === EMPTY_OBJ) { defineReactiveSimple(i.ctx, key, val)
i.data = reactive({})
}
i.data[key] = val
i.accessCache = Object.create(null) i.accessCache = Object.create(null)
} else if (isReactive(obj)) { } else if (isReactive(obj)) {
obj[key] = val obj[key] = val

View File

@ -360,6 +360,20 @@ describe('GLOBAL_PRIVATE_UTIL', () => {
expect(vm.$el.textContent).toBe('2') 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: `<div>{{ $foo }}</div>`
}).$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', () => { test('defineReactive with object value', () => {
const obj: any = {} const obj: any = {}
const val = { a: 1 } const val = { a: 1 }