Vue v-bind的使用

1.src

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <script src="../js/Vue.js" charset="utf-8"></script>
    <script type="text/javascript">
        window.onload = function () {
            var vm = new Vue({
                el: '#box',
                data: {
                    url:'../img/1.png',
                    w100:'100px',
                    title:"这是图片"
                }
            });
        }
    </script>
</head>
<body>
<div id="box">
    <img v-bind:src="url" v-bind:width="w100" v-bind:title="title">
    <img :src="url" :width="w100" :title="title">
</div>
</body>
</html>

2.class []

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style>
    .font-red {
        color: red;
    }
    
    .bgcolor-burlywood {
        background: burlywood;
    }
    </style>
    <script src="../js/Vue.js" charset="utf-8"></script>
    <script type="text/javascript">
    window.onload = function() {
        var vm = new Vue({
            el: '#box',
            data: {
                red: 'font-red',
                burlywood: 'bgcolor-burlywood'
            }
        });
    }
    </script>
</head>

<body>
    <div id="box">
        <p :class="[red,burlywood]">文字颜色</p>
    </div>
</body>

</html>

这里数组中的数据是data中的变量。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        .fontRed{
            color: red;
        }
        .bgBurlywood{
            background: burlywood;
        }
    </style>
    <script src="../js/Vue.js" charset="utf-8"></script>
    <script type="text/javascript">
        window.onload = function () {
            var vm = new Vue({
                el: '#box',
                data: {

                }
            });
        }
    </script>
</head>
<body>
<div id="box">
    <p :class="{fontRed:true,bgBurlywood:true}">文字颜色</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        .fontRed{
            color: red;
        }
        .bgBurlywood{
            background: burlywood;
        }
    </style>
    <script src="../js/Vue.js" charset="utf-8"></script>
    <script type="text/javascript">
        window.onload = function () {
            var vm = new Vue({
                el: '#box',
                data: {
                    r:true,
                    b:true
                }
            });
        }
    </script>
</head>
<body>
<div id="box">
    <p :class="{fontRed:r,bgBurlywood:b}">文字颜色</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        .fontRed{
            color: red;
        }
        .bgBurlywood{
            background: burlywood;
        }
    </style>
    <script src="../js/Vue.js" charset="utf-8"></script>
    <script type="text/javascript">
        window.onload = function () {
            var vm = new Vue({
                el: '#box',
                data: {
                    json:{
                        fontRed:true,
                        bgBurlywood:false
                    }
                }
            });
        }
    </script>
</head>
<body>
<div id="box">
    <p :class="json">文字颜色</p>
</div>
</body>
</html>

方法论:熟练使用其中的常用几种,就可以了!可以进行单元测试!

原文地址:https://www.cnblogs.com/jiqing9006/p/6698160.html