Skip to content

Commit d7de23e

Browse files
authored
fix: should dynamically update refs in context (#764)
1 parent 5cd3003 commit d7de23e

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/mixin.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export function mixin(Vue: VueConstructor) {
3535
},
3636
updated(this: ComponentInstance) {
3737
updateTemplateRef(this)
38+
if (this.$vnode?.context) {
39+
updateTemplateRef(this.$vnode.context)
40+
}
3841
},
3942
})
4043

test/templateRefs.spec.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Vue = require('vue/dist/vue.common.js')
2-
const { ref, watchEffect } = require('../src')
2+
const { ref, watchEffect, nextTick } = require('../src')
33

44
describe('ref', () => {
55
it('should work', (done) => {
@@ -69,6 +69,50 @@ describe('ref', () => {
6969
.then(done)
7070
})
7171

72+
// #296
73+
it('should dynamically update refs in context', async () => {
74+
const vm = new Vue({
75+
setup() {
76+
const barRef = ref(null)
77+
return {
78+
barRef,
79+
}
80+
},
81+
template: `<div>
82+
<foo><bar ref="barRef"/></foo>
83+
</div>`,
84+
components: {
85+
bar: {
86+
setup() {
87+
const name = ref('bar')
88+
return {
89+
name,
90+
}
91+
},
92+
template: '<div> {{name}} </div>',
93+
},
94+
foo: {
95+
setup() {
96+
const showSlot = ref(false)
97+
const setShow = () => {
98+
showSlot.value = true
99+
}
100+
return {
101+
setShow,
102+
showSlot,
103+
}
104+
},
105+
template: `<div> <slot v-if="showSlot"></slot> </div>`,
106+
},
107+
},
108+
}).$mount()
109+
await nextTick()
110+
//@ts-ignore
111+
vm.$children[0].setShow()
112+
await nextTick()
113+
//@ts-ignore
114+
expect(vm.$refs.barRef).toBe(vm.barRef)
115+
})
72116
// TODO: how ?
73117
// it('work with createElement', () => {
74118
// let root;

0 commit comments

Comments
 (0)