Skip to content

Commit

Permalink
百度小程序模板语法适配
Browse files Browse the repository at this point in the history
  • Loading branch information
hucq committed Oct 31, 2018
1 parent a83d817 commit 126a199
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
10 changes: 5 additions & 5 deletions src/platforms/mp/compiler/codegen-swan/config/astMap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default {
if: 'wx:if',
iterator1: 'wx:for-index',
key: 'wx:key',
alias: 'wx:for-item',
'v-for': 'wx:for'
// 'if': 's-if',
'v-for': 's-for',
'alias': 's-for-item',
'iterator1': 's-for-index',
'key': 's-key'
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const noSupport = {
}
export default {
'v-if': {
name: 'wx:if',
type: 0
name: 's-if',
type: 2
},
'v-else-if': {
name: 'wx:elif',
type: 0
name: 's-elif',
type: 2
},
'v-else': {
name: 'wx:else',
name: 'w-else',
type: 1
},
'v-text': {
Expand Down
12 changes: 4 additions & 8 deletions src/platforms/mp/compiler/codegen-swan/convert/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ function getSlotsName (obj) {
// wxml模板中 data="{{ a:{a1:'string2'}, b:'string'}}" 键a不能放在最后,会出错
return tmplateSlotsObj(obj)
.concat(
Object.keys(obj).map(function(k) {
return '$slot' + k + ":'" + obj[k] + "'"
})
Object.keys(obj).map(k => `$slot${k}:'${obj[k]}'`)
)
.join(',')
}
Expand All @@ -18,9 +16,7 @@ function tmplateSlotsObj(obj) {
}
// wxml模板中 data="{{ a:{a1:'string2'}, b:'string'}}" 键a1不能写成 'a1' 带引号的形式,会出错
const $for = Object.keys(obj)
.map(function(k) {
return `${k}:'${obj[k]}'`
})
.map(k => `${k}:'${obj[k]}'`)
.join(',')
return $for ? [`$for:{${$for}}`] : []
}
Expand All @@ -32,7 +28,7 @@ export default {
convertComponent (ast, components, slotName) {
const { attrsMap, tag, mpcomid, slots } = ast
if (slotName) {
attrsMap['data'] = "{{...$root[$k], $root}}"
attrsMap['data'] = "{{{...$root[$k], $root}}}"
// bindedName is available when rendering slot in v-for
const bindedName = attrsMap['v-bind:name']
if(bindedName) {
Expand All @@ -43,7 +39,7 @@ export default {
} else {
const slotsName = getSlotsName(slots)
const restSlotsName = slotsName ? `, ${slotsName}` : ''
attrsMap['data'] = `{{...$root[$kk+${mpcomid}], $root${restSlotsName}}}`
attrsMap['data'] = `{{{...$root[$kk+${mpcomid}], $root${restSlotsName}}}}`
attrsMap['is'] = components[tag].name
}
return ast
Expand Down
26 changes: 15 additions & 11 deletions src/platforms/mp/compiler/codegen-swan/convert/for.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ import astMap from '../config/astMap'
export default function (ast) {
const { iterator1, for: forText, key, alias, attrsMap } = ast

if (forText) {
attrsMap[astMap['v-for']] = `{{${forText}}}`
if (iterator1) {
attrsMap[astMap['iterator1']] = iterator1
}
if (key) {
attrsMap[astMap['key']] = key
}
if (alias) {
attrsMap[astMap['alias']] = alias
}
// 缩写:<view s-for="p,index in persons">
// 全写:<view s-for="persons" s-for-index="index" s-for-item="p">

if (forText) {
attrsMap[astMap['v-for']] = `${alias},${iterator1} in ${forText}`
// attrsMap[astMap['v-for']] = forText
// if (iterator1) {
// attrsMap[astMap['iterator1']] = iterator1
// }
// if (alias) {
// attrsMap[astMap['alias']] = alias
// }
// if (key) {
// attrsMap[astMap['key']] = key
// }
delete attrsMap['v-for']
}


return ast
}
1 change: 0 additions & 1 deletion src/platforms/mp/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import codeGenWx from './codegen/index'
import codeGenSwan from './codegen-swan/index'

function compileToWxml (compiled, options, fileExt) {
debugger
let code
switch(fileExt.platform) {
case 'swan':
Expand Down

0 comments on commit 126a199

Please sign in to comment.