Skip to content

Commit 5851961

Browse files
committed
refactor: adjust codegen for v-if on scoped slot
1 parent 7721feb commit 5851961

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

Diff for: src/compiler/codegen/index.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,10 @@ function genScopedSlots (
360360
slots: { [key: string]: ASTElement },
361361
state: CodegenState
362362
): string {
363-
const hasDynamicKeys = Object.keys(slots).some(key => slots[key].slotTargetDynamic)
363+
const hasDynamicKeys = Object.keys(slots).some(key => {
364+
const slot = slots[key]
365+
return slot.slotTargetDynamic || slot.if || slot.for
366+
})
364367
return `scopedSlots:_u([${
365368
Object.keys(slots).map(key => {
366369
return genScopedSlot(key, slots[key], state)
@@ -373,19 +376,29 @@ function genScopedSlot (
373376
el: ASTElement,
374377
state: CodegenState
375378
): string {
379+
if (el.if && !el.ifProcessed) {
380+
return genIfScopedSlot(key, el, state)
381+
}
376382
if (el.for && !el.forProcessed) {
377383
return genForScopedSlot(key, el, state)
378384
}
379385
const fn = `function(${String(el.slotScope)}){` +
380386
`return ${el.tag === 'template'
381-
? el.if
382-
? `(${el.if})?${genChildren(el, state) || 'undefined'}:undefined`
383-
: genChildren(el, state) || 'undefined'
387+
? genChildren(el, state) || 'undefined'
384388
: genElement(el, state)
385389
}}`
386390
return `{key:${key},fn:${fn}}`
387391
}
388392

393+
function genIfScopedSlot (
394+
key: string,
395+
el: any,
396+
state: CodegenState
397+
): string {
398+
el.ifProcessed = true
399+
return `(${el.if})?${genScopedSlot(key, el, state)}:null`
400+
}
401+
389402
function genForScopedSlot (
390403
key: string,
391404
el: any,

Diff for: src/core/instance/render-helpers/resolve-slots.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function resolveScopedSlots (
5959
const slot = fns[i]
6060
if (Array.isArray(slot)) {
6161
resolveScopedSlots(slot, hasDynamicKeys, res)
62-
} else {
62+
} else if (slot) {
6363
res[slot.key] = slot.fn
6464
}
6565
}

Diff for: test/unit/modules/compiler/codegen.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ describe('codegen', () => {
239239
it('generate scoped slot with multiline v-if', () => {
240240
assertCodegen(
241241
'<foo><template v-if="\nshow\n" slot-scope="bar">{{ bar }}</template></foo>',
242-
`with(this){return _c('foo',{scopedSlots:_u([{key:"default",fn:function(bar){return (\nshow\n)?[_v(_s(bar))]:undefined}}])})}`
242+
`with(this){return _c('foo',{scopedSlots:_u([(\nshow\n)?{key:"default",fn:function(bar){return [_v(_s(bar))]}}:null],true)})}`
243243
)
244244
assertCodegen(
245245
'<foo><div v-if="\nshow\n" slot="foo" slot-scope="bar">{{ bar }}</div></foo>',
246-
`with(this){return _c(\'foo\',{scopedSlots:_u([{key:"foo",fn:function(bar){return (\nshow\n)?_c(\'div\',{},[_v(_s(bar))]):_e()}}])})}`
246+
`with(this){return _c(\'foo\',{scopedSlots:_u([(\nshow\n)?{key:"foo",fn:function(bar){return _c(\'div\',{},[_v(_s(bar))])}}:null],true)})}`
247247
)
248248
})
249249

0 commit comments

Comments
 (0)