Skip to content

Commit 2ce94ff

Browse files
author
songlr
committed
支持 slot[:name] 动态slotname
1 parent 427fedd commit 2ce94ff

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

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}` : ''

0 commit comments

Comments
 (0)