Vue---第二十章class和style,v-bind

1.语法格式

v-bind:class='表达式' 或 :

  class='表达式' class 的表达式可以为:

    字符串 :class="activeClass"

    对象 :class="{active: isActive, error: hasError}"

    数组 :class="['active', 'error']"  注意要加上单引号,不然是获取data中的值


v-bind:style='表达式'或 :

  style='表达式'` style 的表达式一般为

    对象 :style="{color: activeColor, fontSize: fontSize + 'px'}"

    注意:对象中的value值 activeColor 和 fontSize 是data中的属性

例子如下:

<!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>
    <style>
        .active{
            color:blue;
        }
        .delete{
            background-color: red;
        }
        .error{
            font-size: 30px;
        }
    </style>
</head>
<body>
    <div id="app">
        <h3>Class绑定,v-bind:class或:class</h3>
        <!--activeClass会从data中获取值为active,则对应样式为绿色-->
        <p v-bind:class="activeCalss">class字符串表达式1</p>
         <!-- isDelete为 true,渲染delete样式;当 hasError为false,不取error样式;--> 
        <p :class="{delete:idDelete,error:hasError}">class对象表达式2</p>
        <!--- 渲染 'active', 'error' 样式(直接获取style中的样式),注意要加上单引号,不然是获取data中的值 --> 
        <p :class="['active', 'error']">class数组表达式3</p>
        <h2>Style绑定, v-bind:style 或 :class</h2>
        <p :style="{color: activeColor, fontSize: fontSize + 'px'}">Style绑定</p>
    </div>
    <script src="./node_modules/vue/dist/vue.js"></Script>
    <script>
        var vm=new Vue({
            el:"#app",
            data:{
                activeCalss:'active',
                idDelete:true,
                hasError:true,
                //style内容
                activeColor:'red',
                fontSize: 20
            }
        })
    </script>
</body>
</html>

 

 效果

沫笙
原文地址:https://www.cnblogs.com/wendy-0901/p/14412041.html