我正在使用Vue.js,并且想更改CSS类属性。使用该类的HTML代码如下:
<div class="fillTimerBar"></div>
和CSS代码:
.fillTimerBar {
width: 100%;
height: 8px;
}
从那里,我想width
使用computed
Vue组件中的属性更改class属性。
哪一种是正确的方式(如果有)?
您必须使用v-bind:style
指令。
var vm = new Vue({
el: '#example',
data: {
width:'200px'
},
computed: {
computedWidth: function () {
return this.width;
}
},
methods: {
changeWidth: function (event) {
this.width='100px';
}
}
})
#myDiv{
background-color:red;
height:200px;
}
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<div id="example">
<div id="myDiv" v-bind:style="{ width: computedWidth }"></div>
<button v-on:click="changeWidth()">Change</button>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句