Home  >  Article  >  Web Front-end  >  vue.js implements binding class

vue.js implements binding class

小云云
小云云Original
2018-03-26 15:23:071414browse

This article mainly shares with you how to implement binding class in vue.js, mainly in the form of code. I hope it can help you.

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!-- Vue.js -->
    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>
</head>
<body>
<p id="app1">
    <!--# 给v-bind设置一个对象,可以动态地切换class #-->
    <p :class="{&#39;active&#39;:isActive}"></p>
</p>

<p id="app2">
    <!--# 给v-bind设置一个对象,可以动态地切换class #-->
    <!--# 对象中也可以传入多个属性,来动态切换class。另外,:class可以与普通class共存 #-->
    <p class="static" :class="{&#39;active&#39;:isActive,&#39;error&#39;:isError}"></p>
</p>

<p id="app3">
    <!--# 给v-bind设置一个对象,可以动态地切换class #-->
    <!--# 当:class的表达式过长或逻辑复杂时,还可以绑定一个计算属性,这是一种很友好和常见的用法,一般当条件多于两个时,都可以使用data或computer #-->
    <p :class="classes"></p>
</p>
</body>
</html>
<script>
    //例子一
    var app1 = new Vue({
        el:&#39;#app1&#39;,
        data:{
            //类名isActive,当其为true时,p会拥有类名active,为false时则没有
            isActive:true
        }
    });


    //例子二
    var app2 = new Vue({
        el:&#39;#app2&#39;,
        data:{
            //类名isActive,当其为true时,p会拥有类名active,为false时则没有
            isActive:true,
            //类名isError,当其为true时,p会拥有类名error,为false时则没有
            isError:true
        }
    });


    //例子三
    var app3 = new Vue({
        el:&#39;#app3&#39;,
        data:{
            isActive:true,
            isError:false
        },
        computed:{
            classes:function () {

                return {
                    active:this.isActive && !this.isError
                }
                //除了计算属性,你也可以直接绑定一个Object类型的数据,或者使用类似计算机属性的methods
            }
        }
    })
</script>

Related recommendations:

Detailed introduction to Vue.js dynamic binding class

Use v-bind binding in Vue.js Notes on class

Detailed explanation of Vue.js dynamic binding class

The above is the detailed content of vue.js implements binding class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn