Skip to content

Commit 2d64e2f

Browse files
authored
Merge pull request #1088 from songlairui/dynamic-slot-name
Dynamic slot name
2 parents a893e5e + 0612089 commit 2d64e2f

File tree

4 files changed

+71
-12
lines changed

4 files changed

+71
-12
lines changed

packages/mpvue-template-compiler/build.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4567,9 +4567,27 @@ function getSlotsName (obj) {
45674567
if (!obj) {
45684568
return ''
45694569
}
4570-
return Object.keys(obj).map(function (k) {
4571-
return ("$slot" + k + ":'" + (obj[k]) + "'")
4572-
}).join(',')
4570+
// wxml模板中 data="{{ a:{a1:'string2'}, b:'string'}}" 键a不能放在最后,会出错
4571+
return tmplateSlotsObj(obj)
4572+
.concat(
4573+
Object.keys(obj).map(function(k) {
4574+
return '$slot' + k + ":'" + obj[k] + "'"
4575+
})
4576+
)
4577+
.join(',')
4578+
}
4579+
4580+
function tmplateSlotsObj(obj) {
4581+
if (!obj) {
4582+
return []
4583+
}
4584+
// wxml模板中 data="{{ a:{a1:'string2'}, b:'string'}}" 键a1不能写成 'a1' 带引号的形式,会出错
4585+
var $for = Object.keys(obj)
4586+
.map(function(k) {
4587+
return (k + ":'" + (obj[k]) + "'")
4588+
})
4589+
.join(',');
4590+
return $for ? [("$for:{" + $for + "}")] : []
45734591
}
45744592

45754593
var component = {
@@ -4585,7 +4603,13 @@ var component = {
45854603
var slots = ast.slots;
45864604
if (slotName) {
45874605
attrsMap['data'] = "{{...$root[$k], $root}}";
4588-
attrsMap['is'] = "{{" + slotName + "}}";
4606+
// bindedName is available when rendering slot in v-for
4607+
var bindedName = attrsMap['v-bind:name'];
4608+
if(bindedName) {
4609+
attrsMap['is'] = "{{$for[" + bindedName + "]}}";
4610+
} else {
4611+
attrsMap['is'] = "{{" + slotName + "}}";
4612+
}
45894613
} else {
45904614
var slotsName = getSlotsName(slots);
45914615
var restSlotsName = slotsName ? (", " + slotsName) : '';

packages/mpvue/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4148,7 +4148,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
41484148
});
41494149

41504150
Vue$3.version = '2.4.1';
4151-
Vue$3.mpvueVersion = '1.0.12';
4151+
Vue$3.mpvueVersion = '1.0.13';
41524152

41534153
/* globals renderer */
41544154

src/platforms/mp/compiler/codegen/convert/component.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,27 @@ function getSlotsName (obj) {
22
if (!obj) {
33
return ''
44
}
5-
return Object.keys(obj).map(k => {
6-
return `$slot${k}:'${obj[k]}'`
7-
}).join(',')
5+
// wxml模板中 data="{{ a:{a1:'string2'}, b:'string'}}" 键a不能放在最后,会出错
6+
return tmplateSlotsObj(obj)
7+
.concat(
8+
Object.keys(obj).map(function(k) {
9+
return '$slot' + k + ":'" + obj[k] + "'"
10+
})
11+
)
12+
.join(',')
13+
}
14+
15+
function tmplateSlotsObj(obj) {
16+
if (!obj) {
17+
return []
18+
}
19+
// wxml模板中 data="{{ a:{a1:'string2'}, b:'string'}}" 键a1不能写成 'a1' 带引号的形式,会出错
20+
const $for = Object.keys(obj)
21+
.map(function(k) {
22+
return `${k}:'${obj[k]}'`
23+
})
24+
.join(',')
25+
return $for ? [`$for:{${$for}}`] : []
826
}
927

1028
export default {
@@ -14,8 +32,14 @@ export default {
1432
convertComponent (ast, components, slotName) {
1533
const { attrsMap, tag, mpcomid, slots } = ast
1634
if (slotName) {
17-
attrsMap['data'] = `{{...$root[$k], $root}}`
18-
attrsMap['is'] = `{{${slotName}}}`
35+
attrsMap['data'] = "{{...$root[$k], $root}}"
36+
// bindedName is available when rendering slot in v-for
37+
const bindedName = attrsMap['v-bind:name']
38+
if(bindedName) {
39+
attrsMap['is'] = "{{$for[" + bindedName + "]}}"
40+
} else {
41+
attrsMap['is'] = "{{" + slotName + "}}"
42+
}
1943
} else {
2044
const slotsName = getSlotsName(slots)
2145
const restSlotsName = slotsName ? `, ${slotsName}` : ''

test/mp/compiler/index.spec.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,20 @@ describe('slot', () => {
553553
)
554554
})
555555

556+
it('含`:name`的插槽组件', () => {
557+
assertCodegen(
558+
`<div><slot :name="tab.key">test</slot></div>`,
559+
`<template name="a"><view class="_div"><template name="default">test</template><template data="{{...$root[$k], $root}}" is="{{$for[tab.key]}}"></template></view></template>`,
560+
{
561+
name: 'a'
562+
}
563+
)
564+
})
565+
556566
it('slot name', () => {
557567
assertCodegen(
558568
`<card class="baz boo"><a slot="header">test</a></card>`,
559-
`<import src="/components/card" /><template name="a"><template data="{{...$root[$kk+'0'], $root, $slotdefault:'hashValue-default-0',$slotheader:'hashValue-header-0'}}" is="card"></template></template>`,
569+
`<import src="/components/card" /><template name="a"><template data="{{...$root[$kk+'0'], $root, $for:{default:'hashValue-default-0',header:'hashValue-header-0'},$slotdefault:'hashValue-default-0',$slotheader:'hashValue-header-0'}}" is="card"></template></template>`,
560570
{
561571
name: 'a',
562572
components: {
@@ -579,7 +589,7 @@ describe('slot', () => {
579589
it('slot template', () => {
580590
assertCodegen(
581591
`<card class="baz boo"><template slot="header">test</template></card>`,
582-
`<import src="/components/card" /><template name="a"><template data="{{...$root[$kk+'1'], $root, $slotdefault:'hashValue-default-1',$slotheader:'hashValue-header-1'}}" is="card"></template></template>`,
592+
`<import src="/components/card" /><template name="a"><template data="{{...$root[$kk+'1'], $root, $for:{default:'hashValue-default-1',header:'hashValue-header-1'},$slotdefault:'hashValue-default-1',$slotheader:'hashValue-header-1'}}" is="card"></template></template>`,
583593
{
584594
name: 'a',
585595
components: {
@@ -619,6 +629,7 @@ describe('slot', () => {
619629
)
620630
})
621631
})
632+
622633
describe('web-view', () => {
623634
it('web-view', () => {
624635
assertCodegen(

0 commit comments

Comments
 (0)