【发布时间】:2020-06-30 12:46:55
【问题描述】:
以下使用数组语法一次性绑定静态和动态类。静态有效,但动态无效。
动态类绑定如何与 SVG 一起使用?
https://codesandbox.io/s/dynamic-class-binding-svg-in-vue-js-jc5qg
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
.static {
stroke: black;
stroke-width: 1;
fill: yellow;
}
.dynamic {
fill: red;
}
</style>
</head>
<body>
<svg id="app" width="100%" height="100%">
<circle
cx="50" cy="50" r="40"
:class="[ 'static', dynamicToggle ? 'dynamic' : '' ]"
/>
</svg>
<button
@click.prevent="dynamicToggle = ! dynamicToggle"
>toggle color
</button>
<script type="text/javascript">
new Vue({
el: '#app',
data: {
dynamicToggle: false
}
})
</script>
</body>
</html>
【问题讨论】: