v-bind绑定style

90 阅读1分钟

对于绑定style,我们写key value,value是字符,要引号,写字符串形式,不能双引号里面写配置项又写变量,多个配置项写的时候用空格隔开,数组形式,里面的配置项是JSDN类型,多个jey value写在一个对象内也可以,对象是JSDN,但是写key时,如果有-,去掉-。

对于class的时带-,只能加引号不能去-,找不到。但是style可加引号,也可去-

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue (1).js"></script>
    <style>
        .static {
            border:1px solid black;
            width:100px;
            height:100px

        }
       
    </style>
</head>
<body>
    <div id="app">
        <div class="static ">
            123
        </div>
        <br>
        字符串形式
        <div class="static" style="background:gray" >
            123
        </div><br>
        <div class="static" :style="msg">
            123
        </div><br>
        数组形式
        <div class="static" :style="[{background:'green',color:'black'}]" >
            123
        </div><br>
        
        <div class="static" :style="a">
            123
        </div><br>

        对象
        <div class="static" :style='b'>
            123
        </div><br>
    </div>
    <script>
      const vm=  new Vue({
        data(){
            return{
                msg:'background:green',
                a:[{background:'green',color:'black'}],
                b:{
                background:'gray',
                vectorsource:{
                "type": "vector"}
                }
            }
           
        }
        
      ,
      el:"#app"
     } )
    </script>
    
</body>
</html>