🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] >[success] # 样式绑定语法 记下来讲 **2种** 绑定样式的方法,**动态class** 以及 **动态style** 。 >[success] ## class绑定 在 **vue** 中有以下几种方式来 **动态绑定class**: 1. **字符串形式** : **通过字符串来控制样式的显示** 2. **对象形式** :**通过对象来控制样式的显示** 3. **数组形式** :**通过数组来控制样式的显示** 4. **组件上绑定class** :**通过 $attrs 来继承父组件样式** >[success] ### 字符串形式 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>样式绑定语法</title> <style> .red{ color: red; } .green{ color: green; } </style> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ classString: 'red' } }, template: ` <div :class="classString">hello world</div> ` }) const vm = app.mount('#root') </script> </html> ~~~ 然后在 **f12控制台** 输入 ~~~ vm.classString = 'green' ~~~ **文字就会变成绿色** 。 >[success] ### 对象形式 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>样式绑定语法</title> <style> .red{ color: red; } .green{ color: green; } </style> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ classObject: {red:false, green:true} } }, template: ` <div :class="classObject">hello world</div> ` }) const vm = app.mount('#root') </script> </html> ~~~ >[success] ### 数组形式 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>样式绑定语法</title> <style> .red{ color: red; } .green{ color: green; } </style> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ classArray: ['red','green', { brown: true }] } }, template: ` <div :class="classArray">hello world</div> ` }) const vm = app.mount('#root') </script> </html> ~~~ >[success] ### 组件上绑定class **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>样式绑定语法</title> <style> .red{ color: red; } .green{ color: green; } </style> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ classString: 'red', classObject: {red:false, green:true}, classArray: ['red','green', { brown: true }] } }, template: ` <div :class="classString"> hello world <demo class="green"/> </div> ` }) app.component('demo', { template: ` <div class="green">one</div> ` }) const vm = app.mount('#root') </script> </html> ~~~ 如果 **子组件中的template子级只有一个元素** ,可以在 **父组件中子组件标签上添加class** 改变 **子组件样式** ,**子组件template子级有多个元素** ,**vue** 会不知道到底是把 **green** 加到 **第一个节点** ,还是 **第二个节点** ,这种情况可以 **直接在子组件标签身上添加class样式** ,如下: ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>样式绑定语法</title> <style> .red{ color: red; } .green{ color: green; } </style> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ classString: 'red', classObject: {red:false, green:true}, classArray: ['red','green', { brown: true }] } }, template: ` <div :class="classString"> hello world <demo/> </div> ` }) app.component('demo', { template: ` <div class="green">one</div> <div class="green">two</div> ` }) const vm = app.mount('#root') </script> </html> ~~~ 再或者可以使用 **$attrs**,如下代码,这样的写法是什么意思呢,意思:**子组件使用父组件属性上的class的值(子组件继承父组件样式)** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>样式绑定语法</title> <style> .red{ color: red; } .green{ color: green; } </style> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ classString: 'red', classObject: {red:false, green:true}, classArray: ['red','green', { brown: true }] } }, template: ` <div :class="classString"> hello world <demo class="green"/> </div> ` }) app.component('demo', { template: ` <div :class="$attrs.class">one</div> <div :class="$attrs.class">two</div> ` }) const vm = app.mount('#root') </script> </html> ~~~ >[success] ## style绑定 在 **vue** 中有以下几种方式来 **动态绑定style**: 1. **字符串形式**:**通过字符串来控制样式的显示** 2. **对象形式**:**通过对象形式来控制样式的显示** >[success] ### 字符串形式 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>样式绑定语法</title> <style> .red{ color: red; } .green{ color: green; } </style> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ styleString: 'color:yellow' } }, template: ` <div :style="styleString">hello world</div> ` }) const vm = app.mount('#root') </script> </html> ~~~ >[success] ### 对象形式 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>样式绑定语法</title> <style> .red{ color: red; } .green{ color: green; } </style> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ styleObject: { color: 'orange', background: 'yellow' } } }, template: ` <div :style="styleObject">hello world</div> ` }) const vm = app.mount('#root') </script> </html> ~~~