admin管理员组文章数量:1318335
Check this demo below:
new Vue({
el: '#app',
data: {
flag: true
},
puted: {
style() {
let styleA = {
borderBottom: '1px solid red',
borderRight: '1px solid red'
};
let styleB = {
border: '1px solid green',
borderRight: '1px solid red'
}
return this.flag ? styleA : styleB
}
},
methods: {
changeStyle() {
this.flag = !this.flag;
}
}
})
.box {
width: 100px;
height: 100px;
}
<html>
<header>
<script src="/[email protected]/dist/vue.js"></script>
</header>
<body>
<div id="app">
<div class="box" :style="style"></div>
<button @click="changeStyle">changeStyle</button>
</div>
</body>
</html>
Check this demo below:
new Vue({
el: '#app',
data: {
flag: true
},
puted: {
style() {
let styleA = {
borderBottom: '1px solid red',
borderRight: '1px solid red'
};
let styleB = {
border: '1px solid green',
borderRight: '1px solid red'
}
return this.flag ? styleA : styleB
}
},
methods: {
changeStyle() {
this.flag = !this.flag;
}
}
})
.box {
width: 100px;
height: 100px;
}
<html>
<header>
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/vue.js"></script>
</header>
<body>
<div id="app">
<div class="box" :style="style"></div>
<button @click="changeStyle">changeStyle</button>
</div>
</body>
</html>
In this demo, click changeStyle
button to toggle two different styles.
Here is step:
- First,
styleA
is applied withred borderBottom
andred borderRight
Click
changeStyle
button,styleB
is applied,green border
andred borderRight
are expected but only thegreen border
is shown.Click
changeStyle
button again, as we can see, onlyred borderBottom
is shown, likered borderRight
just disappear.Click again, you will never see the
red borderRight
Is something wrong with paring virtual node and rendering in VUE
?
3 Answers
Reset to default 4I don't really know exactly why this happens.
As you said, there's probably something wrong with the virtual DOM.
In my experience, when something is wrong with the DOM rendering in Vue, using key
would solve the problem. In your case, it did. https://jsfiddle/jacobgoh101/Ld5e8azs/
Just add key
to the div with dynamic style
<div class="box" :style="style" :key="style"></div>
key
just needs to be any unique value that differentiates the 2 styles
It is a bug, but this issue was considered a wontfix. This is due to the fact that border
is a shorthand property.
Edit: Jacob beat me by a few secs, but yes, as was stated in the gh issue, the workaround is use key
as a hash to force the render.
Alternative but uglier 'hack' would be to differentiate the borderRight
style, so borderRight: '1px solid red'
in styleA
, and borderRight: '1px solid red '
(notice the whitespace at the end) in styleB
.
It makes vue 'think' that borderRight
has changed, and 'forces' it to apply the style (and 'skips' an optimisation step where it skips applying a style that vue thinks is already applied).
https://jsfiddle/px5qoaed/
本文标签: javascriptborder style not rendering correctly when toggle style in VUEStack Overflow
版权声明:本文标题:javascript - border style not rendering correctly when toggle style in VUE - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742041063a2417548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论