Skip to content

Commit 2533a36

Browse files
committed
fix(template-ref): preserve ref removal behavior in non-composition-api usage
close #12554
1 parent 0fabda7 commit 2533a36

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/core/vdom/modules/template-ref.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export function registerRef(vnode: VNodeWithData, isRemoval?: boolean) {
3333
const vm = vnode.context
3434
const refValue = vnode.componentInstance || vnode.elm
3535
const value = isRemoval ? null : refValue
36+
const $refsValue = isRemoval ? undefined : refValue
3637

3738
if (isFunction(ref)) {
3839
invokeWithErrorHandling(ref, vm, [value], vm, `template ref function`)
@@ -67,14 +68,14 @@ export function registerRef(vnode: VNodeWithData, isRemoval?: boolean) {
6768
if (isRemoval && refs[ref] !== refValue) {
6869
return
6970
}
70-
refs[ref] = value
71+
refs[ref] = $refsValue
7172
setSetupRef(vm, ref, value)
7273
} else if (_isRef) {
7374
if (isRemoval && ref.value !== refValue) {
7475
return
7576
}
7677
ref.value = value
77-
if (setupRefKey) refs[setupRefKey] = value
78+
if (setupRefKey) refs[setupRefKey] = $refsValue
7879
} else if (__DEV__) {
7980
warn(`Invalid template ref type: ${typeof ref}`)
8081
}

test/unit/features/template-ref.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('ref', () => {
5858
expect(vm.$refs.foo).toBe(vm.$el)
5959
vm.value = 'bar'
6060
waitForUpdate(() => {
61-
expect(vm.$refs.foo).toBe(null)
61+
expect(vm.$refs.foo).toBe(undefined)
6262
expect(vm.$refs.bar).toBe(vm.$el)
6363
}).then(done)
6464
})
@@ -101,7 +101,7 @@ describe('ref', () => {
101101
vm.test = ''
102102
})
103103
.then(() => {
104-
expect(vm.$refs.test).toBe(null)
104+
expect(vm.$refs.test).toBe(undefined)
105105
})
106106
.then(done)
107107
})

0 commit comments

Comments
 (0)