class and style are attributes of HTML elements and are used to set the style of the element. We can use v-bind to set the style attributes.
The following example sets the class style in the style tag. There is only one Boolean value isActive in the vue instance, using v-bind:class="{ active: isActive }" Bind the style and decide whether to render based on the Boolean value.
<style>.active { width: 100px; height: 100px; background: green;}</style><p id="app"> <p v-bind:class="{ active: flag}"></p></p><script>new Vue({ el: '#app', data: { flag: true } }) </script>
Bind multiple classes
v-bind:class="{ active: isActive, 'text-danger': hasError }">
Bind multiple classes in the form of objects class
<p id="app"> <p v-bind:class="classObject"></p></p><script>new Vue({ el: '#app', data: { classObject: { active: true, 'text-danger': true } } }) </script>
Bind the style as an array
<p id="app"> <p v-bind:class="[activeClass, errorClass]"></p></p><script>new Vue({ el: '#app', data: { activeClass: 'active', errorClass: 'text-danger' } }) </script>
Inline style Binding
<p id="app"> <p v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">菜鸟教程</p></p><script>new Vue({ el: '#app', data: { activeColor: 'green', fontSize: 30 } })
Bind inline style using object method
<p id="app"> <p v-bind:style="styleObject">菜鸟教程</p></p><script>new Vue({ el: '#app', data: { styleObject: { color: 'green', fontSize: '30px' } } }) </script>
The above is the detailed content of vue.js style binding problem. For more information, please follow other related articles on the PHP Chinese website!