绑定数组的用法

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>4-2绑定数组用法</title>
    <script src="./vue.min.js"></script>
    <style type="text/css">
        .active {
            background-color: aqua;
            width: 30vw;
            height: 30vh;
            border: 10px solid green;
            margin-top: 20px;
        }
        
        .error {
            background-color: blue;
        }
    </style>
</head>

<body>
    <div id="app">
        <div :class="[isActive ? activeCls : '', errorCls]"></div>
        <div :class="[{ 'active': isActive }, errorCls]"></div>
        <button :class="classes">点击</button>
    </div>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                isActive: true,
                activeCls: 'active',
                errorCls: 'error',

                size: 'large',
                disabled: true
            },
            computed: {
                classes: function() {
                    return [
                        'btn', {
                            ['btn-' + this.size]: this.size !== '',
                            ['btn-disabled']: this.disabled
                        }

                    ];
                }
            }
        })
    </script>
</body>

</html>

对象语法

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./vue.min.js"></script>
    <style type="text/css">
        .active {
            background-color: aqua;
            width: 30vw;
            height: 30vh;
            border: 10px solid green;
            margin-top: 20px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div :class="{ 'active': isActive }"></div>
        <div :class="classes"></div>
    </div>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                isActive: true,
                error: null
            },
            computed: {
                classes: function() {
                    return {
                        active: this.isActive && !this.error,
                    }
                }
            }

        })
    </script>
</body>

</html>