Vue. js Basic Learning class and Style Binding

  • 2021-08-05 08:31:35
  • OfStack

Under the banner of consolidating css knowledge, I started the research on vue style binding. Compared with demo in the previous article, there are more styles in this content, which becomes a little fancy. Not much to say, just go to the code:


<html>
<head>
 <meta charset="utf-8">
 <title>Vue test</title>
 <style type="text/css">
 body {font-family: Verdana;}
 p { font-family: Times, "Times New Roman", serif;}
 .static.active {color: green; font-size: 35px;}
 div.text-danger {color: red;font-size: 25px;}
 div.active {color: blue;font-family: Verdana;}
 </style>

 <script src="./vue.min.js"></script>
</head>
<body>

<div id="app">
 <!-- Create an instance of the todo-item component -->
 <todo-item></todo-item>
</div>

<div class="static"
 v-bind:class="{ active: isActive, 'text-danger': hasError }">
 <p>class property set.</p>
</div>

<div id="app3" 
 v-bind:class="[activeClass,errorClass]">
 <p>group class property set.</p>
</div>

<div id="app4" v-bind:class="[isActive ? 'active' : 'text-danger']">
 <p>3 Meta-expression plus style </p>
</div>

<div id="app5">
 <my-component v-bind:class="{ active: isActive }"></my-component> 
</div>

<div id="app6">
 <p v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"> Bind inline style </p>
 <p v-bind:style="styleObject"> Object style binding </p>
</div>

<script>

Vue.component('todo-item', {
 template: '<p>todo test.</p>'
})

// 1 Must be instantiated to use 
var app = new Vue({
 el: '#app'
})

//  Construct with class selector 1 A Vue Object and bind out of quota class Attribute 
var app2 = new Vue({
 el: '.static',
 data: {
 isActive: false,
 hasError: true
 }
})

//  Array syntax plus  class ( Because the style is taken from bottom to top, so text-danger Adj. color Style overridden )
var app3 = new Vue({
 el: '#app3',
 data: {
 activeClass: 'active',
 errorClass: 'text-danger'
 }
})

var app4 = new Vue({
 el: '#app4',
 data: {
 isActive: true
 }
})

Vue.component('my-component',{
 template: '<p class="static"> Add styles to custom components that have already defined styles </p>'
})
var app5 = new Vue({
 el: '#app5',
 data: {
 isActive: true
 }
})

//  Bind inline style 
var app6 = new Vue({
 el: '#app6',
 data: {
 activeColor: '#FF00FF',
 fontSize: 30,
 styleObject: {
  color: '#585858',
  fontSize: '25px'
 }
 }
})



</script>

</body>

</html>

Related articles: