Skip to content

Commit

Permalink
fix: should dynamically update refs in context (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Jul 16, 2021
1 parent 5cd3003 commit d7de23e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/mixin.ts
Expand Up @@ -35,6 +35,9 @@ export function mixin(Vue: VueConstructor) {
},
updated(this: ComponentInstance) {
updateTemplateRef(this)
if (this.$vnode?.context) {
updateTemplateRef(this.$vnode.context)
}
},
})

Expand Down
46 changes: 45 additions & 1 deletion test/templateRefs.spec.js
@@ -1,5 +1,5 @@
const Vue = require('vue/dist/vue.common.js')
const { ref, watchEffect } = require('../src')
const { ref, watchEffect, nextTick } = require('../src')

describe('ref', () => {
it('should work', (done) => {
Expand Down Expand Up @@ -69,6 +69,50 @@ describe('ref', () => {
.then(done)
})

// #296
it('should dynamically update refs in context', async () => {
const vm = new Vue({
setup() {
const barRef = ref(null)
return {
barRef,
}
},
template: `<div>
<foo><bar ref="barRef"/></foo>
</div>`,
components: {
bar: {
setup() {
const name = ref('bar')
return {
name,
}
},
template: '<div> {{name}} </div>',
},
foo: {
setup() {
const showSlot = ref(false)
const setShow = () => {
showSlot.value = true
}
return {
setShow,
showSlot,
}
},
template: `<div> <slot v-if="showSlot"></slot> </div>`,
},
},
}).$mount()
await nextTick()
//@ts-ignore
vm.$children[0].setShow()
await nextTick()
//@ts-ignore
expect(vm.$refs.barRef).toBe(vm.barRef)
})
// TODO: how ?
// it('work with createElement', () => {
// let root;
Expand Down

0 comments on commit d7de23e

Please sign in to comment.