1. class样式绑定1
<! DOCTYPE html >
< html lang = " en" > < head> < meta charset = " UTF-8" > < meta http-equiv = " X-UA-Compatible" content = " IE=edge" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Document</ title> < script src = " ../vue.js" > </ script> < style> div { width : 200px; height : 50px; } .damao { color : red; } .ermao { background-color : green; } .FirstClass { border : thin solid black; } .SecondClass { background-color : yellow; } </ style>
</ head> < body> < div id = " app" > < div v-bind: class= ' {damao: true, ermao: true}' > 直接写入类名</ div> < div v-bind: class= ' {damao: isDamao, ermao: isErmao}' > 你好</ div> < button v-on: click= ' handle' > 点击</ button> < hr> < div v-bind: class= ' [" FirstClass" , " SecondClass" ]' > 直接写入类名</ div> < div v-bind: class= ' [First, Second]' > 通过data对象来对外暴露</ div> < button v-on: click= ' handle_Array' > 点击</ button> </ div> < script> var vm = new Vue ( { el : '#app' , data : { isDamao : true , isErmao : true , First : 'FirstClass' , Second : 'SecondClass' } , methods : { handle : function ( ) { this . isDamao = ! this . isDamao; this . isErmao = ! this . isErmao; } , handle_Array : function ( ) { this . First = '' ; this . Second = '' ; } } } ) ; </ script>
</ body>
</ html>
2. class样式绑定2
<! DOCTYPE html >
< html lang = " en" > < head> < meta charset = " UTF-8" > < meta http-equiv = " X-UA-Compatible" content = " IE=edge" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Document</ title> < script src = " ../vue.js" > </ script> < style> .damao { color : red; } .FirstClass { border : thin solid black; } .SecondClass { background-color : yellow; } .Font { color : green; font-size : 20px; } </ style>
</ head> < body> < div id = " app" > < div v-bind: class= ' [First, Second, {damao: true}]' > 你好</ div> < br> < div v-bind: class= ' array' > 你好</ div> < div v-bind: class= ' obj' > 他好</ div> < br> < div class = " Font" v-bind: class= ' array' > 都好</ div> </ div> < script> var vm = new Vue ( { el : '#app' , data : { First : 'FirstClass' , Second : 'SecondClass' , array : [ 'FirstClass' , 'SecondClass' ] , obj : { FirstClass : true , SecondClass : true , } } } ) ; </ script>
</ body> </ html>
3. class样式绑定3
<! DOCTYPE html >
< html lang = " en" > < head> < meta charset = " UTF-8" > < meta http-equiv = " X-UA-Compatible" content = " IE=edge" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Document</ title> < script src = " ../vue.js" > </ script> < style> .base { width : 100px; height : 100px; } .safe { background-color : green; } .danger { background-color : red; } </ style>
</ head> < body> < div id = " app" > < div v-bind: class= " classObject" > </ div> </ div> < script> var vm = new Vue ( { el : '#app' , data : { isActive : true , error : { value : true , type : 'fatal' } } , methods : { } , computed : { classObject : function ( ) { return { base : true , safe : this . isActive && ! this . error. value, danger : this . error. value && this . error. type === 'fatal' , } } } , watch : { } } ) </ script>
</ body> </ html>
4. style样式绑定1
<! DOCTYPE html >
< html lang = " en" > < head> < meta charset = " UTF-8" > < meta http-equiv = " X-UA-Compatible" content = " IE=edge" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Document</ title> < script src = " ../vue.js" > </ script>
</ head> < body> < div id = " app" > < div :style = ' { border: borderStyle, width:widthStyle, height:heightStyle }' > 1</ div> < div :style = ' objStyle' > 2</ div> < div :style = ' [firstStyles, secondStyles]' > 3</ div> </ div> < script> var vm = new Vue ( { el : '#app' , data : { borderStyle : 'thin solid red' , widthStyle : '100px' , heightStyle : '200px' , objStyle : { border : 'thin solid blue' , width : '200px' , height : '100px' , } , firstStyles : { color : 'green' } , secondStyles : { backgroundColor : 'yellow' } } , methods : { handle : function ( ) { } } } ) ; </ script>
</ body> </ html>
5. style样式绑定2
<! DOCTYPE html >
< html lang = " en" > < head> < meta charset = " UTF-8" > < meta http-equiv = " X-UA-Compatible" content = " IE=edge" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Document</ title> < script src = " ../vue.js" > </ script> < style> div { width : 200px; height : 100px; } </ style>
</ head> < body> < div id = " app" > < div v-bind: style= " { color: activeColor, ' font-size' : fontSize + ' px' }" > 短横线形式</ div> < div v-bind: style= " { color: activeColor, fontSize: fontSize + ' px' }" > 驼峰形式</ div> < div v-bind: style= " styleObject" > 都是菜鸟</ div> < div v-bind: style= " [styleObject,overrideStyle]" > 好吧</ div> < div @click = ' test(2,3)' style = " background-color : yellow" > </ div> </ div> < script> var vm = new Vue ( { el : '#app' , data : { activeColor : 'green' , fontSize : 30 , styleObject : { color : 'red' , fontSize : '30px' } , overrideStyle : { 'fontWeight' : 'bolder' } } , methods : { test ( num1, num2 ) { alert ( num1 + num2) ; } } , computed : { } , watch : { } } ) ; </ script>
</ body> </ html>
6. 其他
<! DOCTYPE html >
< html lang = " en" > < head> < meta charset = " UTF-8" > < meta http-equiv = " X-UA-Compatible" content = " IE=edge" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Document</ title> < script src = " ../vue.js" > </ script> < style> .use { color : green; background-color : red; } .after { color : black; } </ style>
</ head> < body> < div id = " app" > < self-com class = " after" > </ self-com> </ div> < script> Vue. component ( 'self-com' , { template : ` <h1 class="use">组件使用class绑定</h1> ` } ) var vm = new Vue ( { el : '#app' , data : { } , methods : { } , computed : { } , watch : { } } ) </ script>
</ body> </ html>