Skip to content

Commit b79a06c

Browse files
committed
wip: optimize expose
1 parent a603d56 commit b79a06c

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

packages/runtime-core/__tests__/apiExpose.spec.ts

+46
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,50 @@ describe('api: expose', () => {
9595
expect(childRef.value.bar).toBe(2)
9696
expect(childRef.value.baz).toBeUndefined()
9797
})
98+
99+
test('options: empty', () => {
100+
const Child = defineComponent({
101+
render() {},
102+
expose: [],
103+
data() {
104+
return {
105+
foo: 1
106+
}
107+
}
108+
})
109+
110+
const childRef = ref()
111+
const Parent = {
112+
setup() {
113+
return () => h(Child, { ref: childRef })
114+
}
115+
}
116+
const root = nodeOps.createElement('div')
117+
render(h(Parent), root)
118+
expect(childRef.value).toBeTruthy()
119+
expect('foo' in childRef.value).toBe(false)
120+
})
121+
122+
test('options: empty + setup context', () => {
123+
const Child = defineComponent({
124+
render() {},
125+
expose: [],
126+
setup(_, { expose }) {
127+
expose({
128+
foo: 1
129+
})
130+
}
131+
})
132+
133+
const childRef = ref()
134+
const Parent = {
135+
setup() {
136+
return () => h(Child, { ref: childRef })
137+
}
138+
}
139+
const root = nodeOps.createElement('div')
140+
render(h(Parent), root)
141+
expect(childRef.value).toBeTruthy()
142+
expect(childRef.value.foo).toBe(1)
143+
})
98144
})

packages/runtime-core/src/componentOptions.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -743,11 +743,19 @@ export function applyOptions(
743743
onUnmounted(unmounted.bind(publicThis))
744744
}
745745

746-
if (!asMixin && expose) {
747-
const exposed = instance.exposed || (instance.exposed = proxyRefs({}))
748-
expose.forEach(key => {
749-
exposed[key] = toRef(publicThis, key as any)
750-
})
746+
if (isArray(expose)) {
747+
if (!asMixin) {
748+
if (expose.length) {
749+
const exposed = instance.exposed || (instance.exposed = proxyRefs({}))
750+
expose.forEach(key => {
751+
exposed[key] = toRef(publicThis, key as any)
752+
})
753+
} else if (!instance.exposed) {
754+
instance.exposed = EMPTY_OBJ
755+
}
756+
} else if (__DEV__) {
757+
warn(`The \`expose\` option is ignored when used in mixins.`)
758+
}
751759
}
752760
}
753761

0 commit comments

Comments
 (0)