File tree 2 files changed +44
-5
lines changed
2 files changed +44
-5
lines changed Original file line number Diff line number Diff line change @@ -157,6 +157,40 @@ describe('component: slots', () => {
157
157
} )
158
158
159
159
test ( 'the current instance should be kept in the slot' , async ( ) => {
160
+ let instanceInDefaultSlot : any
161
+ let instanceInFooSlot : any
162
+
163
+ const Comp = defineComponent ( {
164
+ render ( ) {
165
+ const instance = getCurrentInstance ( )
166
+ instance ! . slots . default ! ( )
167
+ instance ! . slots . foo ! ( )
168
+ return template ( '<div></div>' ) ( )
169
+ } ,
170
+ } )
171
+
172
+ const { instance } = define ( {
173
+ render ( ) {
174
+ return createComponent ( Comp , { } , [
175
+ {
176
+ default : ( ) => {
177
+ instanceInDefaultSlot = getCurrentInstance ( )
178
+ return template ( 'content' ) ( )
179
+ } ,
180
+ foo : ( ) => {
181
+ instanceInFooSlot = getCurrentInstance ( )
182
+ return template ( 'content' ) ( )
183
+ } ,
184
+ } ,
185
+ ] )
186
+ } ,
187
+ } ) . render ( )
188
+
189
+ expect ( instanceInDefaultSlot ) . toBe ( instance )
190
+ expect ( instanceInFooSlot ) . toBe ( instance )
191
+ } )
192
+
193
+ test ( 'the current instance should be kept in the dynamic slots' , async ( ) => {
160
194
let instanceInDefaultSlot : any
161
195
let instanceInVForSlot : any
162
196
let instanceInVIfSlot : any
Original file line number Diff line number Diff line change @@ -40,12 +40,17 @@ export function initSlots(
40
40
if ( ! rawSlots ) return
41
41
if ( ! isArray ( rawSlots ) ) rawSlots = [ rawSlots ]
42
42
43
- if ( rawSlots . length === 1 && ! isDynamicSlotFn ( rawSlots [ 0 ] ) ) {
44
- instance . slots = rawSlots [ 0 ]
43
+ if ( ! rawSlots . some ( slot => isDynamicSlotFn ( slot ) ) ) {
44
+ instance . slots = { }
45
+ // with ctx
46
+ const slots = rawSlots [ 0 ] as StaticSlots
47
+ for ( const name in slots ) {
48
+ registerSlot ( name , slots [ name ] )
49
+ }
45
50
return
46
51
}
47
52
48
- const resolved : StaticSlots = ( instance . slots = shallowReactive ( { } ) )
53
+ instance . slots = shallowReactive ( { } )
49
54
const keys : Set < string > [ ] = [ ]
50
55
rawSlots . forEach ( ( slots , index ) => {
51
56
const isDynamicSlot = isDynamicSlotFn ( slots )
@@ -71,7 +76,7 @@ export function initSlots(
71
76
: dynamicSlot && dynamicSlot . name === name )
72
77
) {
73
78
recordNames . delete ( name )
74
- delete resolved [ name ]
79
+ delete instance . slots [ name ]
75
80
}
76
81
}
77
82
} )
@@ -83,7 +88,7 @@ export function initSlots(
83
88
} )
84
89
85
90
function registerSlot ( name : string , fn : Slot , recordNames ?: Set < string > ) {
86
- resolved [ name ] = withCtx ( fn )
91
+ instance . slots [ name ] = withCtx ( fn )
87
92
recordNames && recordNames . add ( name )
88
93
}
89
94
You can’t perform that action at this time.
0 commit comments