Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime): Addressed issue with $parent chain when using expose() #4048

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/runtime-core/__tests__/apiExpose.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,20 @@ describe('api: expose', () => {
})

test('expose should allow access to built-in instance properties', () => {
const GrandChild = defineComponent({
render() {
return h('div')
}
})

const grandChildRef = ref()
const Child = defineComponent({
render() {
return h('div')
},
setup(_, { expose }) {
expose()
return {}
return () => h(GrandChild, { ref: grandChildRef })
}
})

Expand All @@ -190,5 +197,7 @@ describe('api: expose', () => {
const root = nodeOps.createElement('div')
render(h(Parent), root)
expect(childRef.value.$el.tag).toBe('div')
expect(grandChildRef.value.$parent).toBe(childRef.value)
expect(grandChildRef.value.$parent.$parent).toBe(grandChildRef.value.$root)
})
})
3 changes: 2 additions & 1 deletion packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ComponentInternalInstance,
Data,
getExposeProxy,
isStatefulComponent
} from './component'
import { nextTick, queueJob } from './scheduler'
Expand Down Expand Up @@ -217,7 +218,7 @@ const getPublicInstance = (
i: ComponentInternalInstance | null
): ComponentPublicInstance | ComponentInternalInstance['exposed'] | 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)
}

Expand Down