Skip to content

Commit eee73cf

Browse files
authored
Merge pull request #391 from F-loat/fix-dynamic-style
fix: #197 修正 style 对象转换后的运算优先级
2 parents 7c5729c + 65b314f commit eee73cf

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/platforms/mp/compiler/codegen/babel-plugins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const objectToStringVisitor = {
3333
const keyStr = getStrByNode(propertyItem.key, true)
3434
const key = keyStr ? hyphenate(keyStr) : keyStr
3535
const { code: val } = generate(t.ExpressionStatement(propertyItem.value))
36-
return `'${key}:' + ${val.slice(0, -1)} + ';'`
36+
return `'${key}:' + (${val.slice(0, -1)}) + ';'`
3737
}).join('+')
3838

3939
const p = template(expression)({})

test/mp/compiler/index.spec.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,17 @@ describe('指令', () => {
217217
it('v-bind:style', () => {
218218
assertCodegen(
219219
`<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">111</div>`,
220-
`<template name="a"><view class="_div" style=" {{('color:' + activeColor + ';' + 'font-size:' + fontSize + 'px' + ';')}}">111</view></template>`,
220+
`<template name="a"><view class="_div" style=" {{('color:' + activeColor + ';' + 'font-size:' + (fontSize + 'px') + ';')}}">111</view></template>`,
221+
{ name: 'a' }
222+
)
223+
assertCodegen(
224+
`<div v-bind:style="{ color: a === b ? activeColor : color, fontSize: fontSize + 'px' }">111</div>`,
225+
`<template name="a"><view class="_div" style=" {{( 'color:' + (a === b ? activeColor : color) + ';' + 'font-size:' + (fontSize + 'px') + ';')}}">111</view></template>`,
221226
{ name: 'a' }
222227
)
223228
assertCodegen(
224229
`<div v-bind:style="[{ color: activeColor, fontSize: fontSize + 'px' }]">111</div>`,
225-
`<template name="a"><view class="_div" style=" {{['color:' + activeColor + ';' + 'font-size:' + fontSize + 'px' + ';']}}">111</view></template>`,
230+
`<template name="a"><view class="_div" style=" {{['color:' + activeColor + ';' + 'font-size:' + (fontSize + 'px') + ';']}}">111</view></template>`,
226231
{ name: 'a' }
227232
)
228233
assertCodegen(

0 commit comments

Comments
 (0)