Skip to content

Commit 8f7c49c

Browse files
defccyyx990803
authored andcommitted
Remove old static style when applying style update (fix vuejs#4227) (vuejs#4235)
* both static style and stylebinding should be removed * update test case * update test case
1 parent f802317 commit 8f7c49c

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/platforms/web/runtime/modules/style.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ function updateStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {
4242

4343
let cur, name
4444
const el: any = vnode.elm
45-
const oldStyle: any = oldVnode.data.style || {}
45+
const oldStaticStyle: any = oldVnode.data.staticStyle
46+
const oldStyleBinding: any = oldVnode.data.style || {}
47+
48+
// if static style exists, stylebinding already merged into it when doing normalizeStyleData
49+
const oldStyle = oldStaticStyle || oldStyleBinding
50+
4651
const style = normalizeStyleBinding(vnode.data.style) || {}
4752

4853
vnode.data.style = style.__ob__ ? extend({}, style) : style

test/unit/features/directives/style.spec.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,28 @@ describe('Directive v-bind:style', () => {
280280

281281
it('should not merge for different adjacent elements', (done) => {
282282
const vm = new Vue({
283-
template: '<div>' +
284-
'<section style="color: blue" v-if="!bool"></section>' +
285-
'<div></div>' +
286-
'<section style="margin: 0" v-if="bool"></section>' +
287-
'</div>',
283+
template:
284+
'<div>' +
285+
'<section style="color: blue" :style="style" v-if="!bool"></section>' +
286+
'<div></div>' +
287+
'<section style="margin-top: 12px" v-if="bool"></section>' +
288+
'</div>',
288289
data: {
289-
bool: false
290+
bool: false,
291+
style: {
292+
fontSize: '12px'
293+
}
290294
}
291295
}).$mount()
296+
const style = vm.$el.children[0].style
297+
expect(style.fontSize).toBe('12px')
298+
expect(style.color).toBe('blue')
292299
waitForUpdate(() => {
293300
vm.bool = true
294301
}).then(() => {
295-
expect(vm.$el.children[1].style.color).not.toBe('blue')
302+
expect(style.color).toBe('')
303+
expect(style.fontSize).toBe('')
304+
expect(style.marginTop).toBe('12px')
296305
}).then(done)
297306
})
298307
})

0 commit comments

Comments
 (0)