feat(expose): always expose $ instance properties on child refs

This commit is contained in:
Evan You
2021-06-24 21:28:09 -04:00
parent a5a66c5196
commit b0203a3092
5 changed files with 75 additions and 29 deletions

View File

@@ -14,7 +14,8 @@ import {
createRenderContext,
exposePropsOnRenderContext,
exposeSetupStateOnRenderContext,
ComponentPublicInstanceConstructor
ComponentPublicInstanceConstructor,
publicPropertiesMap
} from './componentPublicInstance'
import {
ComponentPropsOptions,
@@ -169,7 +170,7 @@ export interface SetupContext<E = EmitsOptions> {
attrs: Data
slots: Slots
emit: EmitFn<E>
expose: (exposed: Record<string, any>) => void
expose: (exposed?: Record<string, any>) => void
}
/**
@@ -291,6 +292,7 @@ export interface ComponentInternalInstance {
// exposed properties via expose()
exposed: Record<string, any> | null
exposeProxy: Record<string, any> | null
/**
* alternative proxy used only for runtime-compiled render functions using
@@ -447,6 +449,7 @@ export function createComponentInstance(
render: null,
proxy: null,
exposed: null,
exposeProxy: null,
withProxy: null,
effects: null,
provides: parent ? parent.provides : Object.create(appContext.provides),
@@ -837,7 +840,7 @@ export function createSetupContext(
if (__DEV__ && instance.exposed) {
warn(`expose() should be called only once per setup().`)
}
instance.exposed = proxyRefs(exposed)
instance.exposed = exposed || {}
}
if (__DEV__) {
@@ -868,6 +871,23 @@ export function createSetupContext(
}
}
export function getExposeProxy(instance: ComponentInternalInstance) {
if (instance.exposed) {
return (
instance.exposeProxy ||
(instance.exposeProxy = new Proxy(proxyRefs(markRaw(instance.exposed)), {
get(target, key: string) {
if (key in target) {
return target[key]
} else if (key in publicPropertiesMap) {
return publicPropertiesMap[key](instance)
}
}
}))
)
}
}
// record effects created during a component's setup() so that they can be
// stopped when the component unmounts
export function recordInstanceBoundEffect(