1
- import { EffectScope } from '@vue/reactivity'
2
- import { EMPTY_OBJ , NOOP , isFunction } from '@vue/shared'
1
+ import { EffectScope , isRef } from '@vue/reactivity'
2
+ import { EMPTY_OBJ , isArray , isFunction } from '@vue/shared'
3
3
import type { Block } from './apiRender'
4
4
import type { DirectiveBinding } from './directives'
5
5
import {
@@ -45,6 +45,30 @@ export type SetupContext<E = EmitsOptions> = E extends any
45
45
export function createSetupContext (
46
46
instance : ComponentInternalInstance ,
47
47
) : SetupContext {
48
+ const expose : SetupContext [ 'expose' ] = exposed => {
49
+ if ( __DEV__ ) {
50
+ if ( instance . exposed ) {
51
+ warn ( `expose() should be called only once per setup().` )
52
+ }
53
+ if ( exposed != null ) {
54
+ let exposedType : string = typeof exposed
55
+ if ( exposedType === 'object' ) {
56
+ if ( isArray ( exposed ) ) {
57
+ exposedType = 'array'
58
+ } else if ( isRef ( exposed ) ) {
59
+ exposedType = 'ref'
60
+ }
61
+ }
62
+ if ( exposedType !== 'object' ) {
63
+ warn (
64
+ `expose() should be passed a plain object, received ${ exposedType } .` ,
65
+ )
66
+ }
67
+ }
68
+ }
69
+ instance . exposed = exposed || { }
70
+ }
71
+
48
72
if ( __DEV__ ) {
49
73
// We use getters in dev in case libs like test-utils overwrite instance
50
74
// properties (overwrites should not be done in prod)
@@ -58,7 +82,7 @@ export function createSetupContext(
58
82
get emit ( ) {
59
83
return ( event : string , ...args : any [ ] ) => instance . emit ( event , ...args )
60
84
} ,
61
- expose : NOOP ,
85
+ expose,
62
86
} )
63
87
} else {
64
88
return {
@@ -67,7 +91,7 @@ export function createSetupContext(
67
91
} ,
68
92
emit : instance . emit ,
69
93
slots : instance . slots ,
70
- expose : NOOP ,
94
+ expose,
71
95
}
72
96
}
73
97
}
@@ -114,9 +138,12 @@ export interface ComponentInternalInstance {
114
138
attrs : Data
115
139
slots : InternalSlots
116
140
refs : Data
141
+ // exposed properties via expose()
142
+ exposed : Record < string , any > | null
117
143
118
144
attrsProxy ?: Data
119
145
slotsProxy ?: Slots
146
+ exposeProxy : Record < string , any > | null
120
147
121
148
// lifecycle
122
149
isMounted : boolean
@@ -238,6 +265,8 @@ export function createComponentInstance(
238
265
attrs : EMPTY_OBJ ,
239
266
slots : EMPTY_OBJ ,
240
267
refs : EMPTY_OBJ ,
268
+ exposed : null ,
269
+ exposeProxy : null ,
241
270
242
271
// lifecycle
243
272
isMounted : false ,
0 commit comments