fix(reactivity): replaced ref in reactive object should be tracked (#1058)

This commit is contained in:
Yang Mingshan 2020-06-12 21:20:43 +08:00 committed by GitHub
parent d437a0145d
commit 80e1693e1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -131,6 +131,21 @@ describe('reactivity/reactive', () => {
expect(typeof obj.b).toBe(`number`) expect(typeof obj.b).toBe(`number`)
}) })
test('should allow setting property from a ref to another ref', () => {
const foo = ref(0)
const bar = ref(1)
const observed = reactive({ a: foo })
const dummy = computed(() => observed.a)
expect(dummy.value).toBe(0)
// @ts-ignore
observed.a = bar
expect(dummy.value).toBe(1)
bar.value++
expect(dummy.value).toBe(2)
})
test('non-observable values', () => { test('non-observable values', () => {
const assertValue = (value: any) => { const assertValue = (value: any) => {
reactive(value) reactive(value)

View File

@ -66,14 +66,14 @@ function createGetter(isReadonly = false, shallow = false) {
return res return res
} }
if (shallow) {
!isReadonly && track(target, TrackOpTypes.GET, key) !isReadonly && track(target, TrackOpTypes.GET, key)
if (shallow) {
return res return res
} }
if (isRef(res)) { if (isRef(res)) {
if (targetIsArray) { if (targetIsArray) {
!isReadonly && track(target, TrackOpTypes.GET, key)
return res return res
} else { } else {
// ref unwrapping, only for Objects, not for Arrays. // ref unwrapping, only for Objects, not for Arrays.
@ -81,7 +81,6 @@ function createGetter(isReadonly = false, shallow = false) {
} }
} }
!isReadonly && track(target, TrackOpTypes.GET, key)
return isObject(res) return isObject(res)
? isReadonly ? isReadonly
? // need to lazy access readonly and reactive here to avoid ? // need to lazy access readonly and reactive here to avoid