fix(runtime-core): fix instance accessed via $parent chain when using expose() (#4048)

This commit is contained in:
Austin Keener 2021-07-02 07:51:54 -04:00 committed by GitHub
parent 735ada1507
commit 12cf9f4ea1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -171,13 +171,20 @@ describe('api: expose', () => {
}) })
test('expose should allow access to built-in instance properties', () => { test('expose should allow access to built-in instance properties', () => {
const GrandChild = defineComponent({
render() {
return h('div')
}
})
const grandChildRef = ref()
const Child = defineComponent({ const Child = defineComponent({
render() { render() {
return h('div') return h('div')
}, },
setup(_, { expose }) { setup(_, { expose }) {
expose() expose()
return {} return () => h(GrandChild, { ref: grandChildRef })
} }
}) })
@ -190,5 +197,7 @@ describe('api: expose', () => {
const root = nodeOps.createElement('div') const root = nodeOps.createElement('div')
render(h(Parent), root) render(h(Parent), root)
expect(childRef.value.$el.tag).toBe('div') expect(childRef.value.$el.tag).toBe('div')
expect(grandChildRef.value.$parent).toBe(childRef.value)
expect(grandChildRef.value.$parent.$parent).toBe(grandChildRef.value.$root)
}) })
}) })

View File

@ -1,6 +1,7 @@
import { import {
ComponentInternalInstance, ComponentInternalInstance,
Data, Data,
getExposeProxy,
isStatefulComponent isStatefulComponent
} from './component' } from './component'
import { nextTick, queueJob } from './scheduler' import { nextTick, queueJob } from './scheduler'
@ -217,7 +218,7 @@ const getPublicInstance = (
i: ComponentInternalInstance | null i: ComponentInternalInstance | null
): ComponentPublicInstance | ComponentInternalInstance['exposed'] | null => { ): ComponentPublicInstance | ComponentInternalInstance['exposed'] | null => {
if (!i) return null if (!i) return null
if (isStatefulComponent(i)) return i.exposed ? i.exposed : i.proxy if (isStatefulComponent(i)) return getExposeProxy(i) || i.proxy
return getPublicInstance(i.parent) return getPublicInstance(i.parent)
} }