Vue3学习笔记(9.0)

32 篇文章 0 订阅
23 篇文章 0 订阅

Vue3样式绑定

class与style是HTML元素的属性,用于设置元素的样式,我们可以用v-bind来设置样式属性。

v-bind在处理class和style时,表达式除了可以使用字符串外,还可以是对象或数组

v-bind:class可以简写为:class

class属性绑定

我们可以用v-bind:class设置一个对象,从而动态的切换class:

<!--
 * @Author: RealRoad1083425287@qq.com
 * @Date: 2023-04-02 19:41:53
 * @LastEditors: Mei
 * @LastEditTime: 2023-04-03 15:02:46
 * @FilePath: \vscode\Vue3_listen.html
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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>
    <script src="vue_doc/vue.global3.js"></script>
    <style>
        .active{
        width:100px;
        height:100px;
        background:blue;
    }
    </style>
   
</head>
<body>
    <div id="app">
        <!-- <p style = "font-size: 25px;">计数器:{{counter}}</p>
        <button @click="counter++" style="font-size: 25px;">点我</button> -->
        <!-- 千米:<input type="text" v-model="kilometers" @focus="currentlyActiveField='kilometers'">
        米:<input type="text" v-model="meters" @focus="currentlyActiveField='meters'"> -->
        <div :class="{'active':isActive}"></div>

    </div>

    <!-- <p id="info"></p> -->

    <script>
        const app={
            data(){
                return{
                    // counter:1
                    // kilometers:0,
                    // meters:0
                    isActive:true
                }
            }
        }
        //     watch:{
        //         meters:function(newValue,oldValue){
        //             if(this.currentlyActiveField==='meters'){
        //                 this.kilometers=newValue/1000;
        //                 this.meters=newValue;
        //             }
        //         },
        //         kilometers:function(newValue,oldValue){
        //             if(this.currentlyActiveField==='kilometers'){
        //                 this.kilometers=newValue;
        //                 this.meters=newValue*1000;
        //             }
        //         }
        //     }
        // }

        Vue.createApp(app).mount('#app')
        // vm.$watch('kilometers',function(a,b){
        //     document.getElementById("info").innerHTML='数字变化,修改前的值是:'+b+'修改后的值是'+a+'!!!!!!'
        // })
    </script>
</body>
</html>

 我们也可以在对象中传入更多属性用来动态切换多个class。

此外,:class指令也可以与普通的class属性共存。

<!--
 * @Author: RealRoad1083425287@qq.com
 * @Date: 2023-04-02 19:41:53
 * @LastEditors: Mei
 * @LastEditTime: 2023-04-03 15:10:21
 * @FilePath: \vscode\Vue3_listen.html
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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>
    <script src="vue_doc/vue.global3.js"></script>
    <style>
        .active{
        /* width:100px;
        height:100px; */
        background:blue;
    }
    .static{
        width: 100px;
        height:100px
    }
    .text_danger{
        background: red;
    }
    </style>
   
</head>
<body>
    <div id="app">
        <!-- <p style = "font-size: 25px;">计数器:{{counter}}</p>
        <button @click="counter++" style="font-size: 25px;">点我</button> -->
        <!-- 千米:<input type="text" v-model="kilometers" @focus="currentlyActiveField='kilometers'">
        米:<input type="text" v-model="meters" @focus="currentlyActiveField='meters'"> -->
        <div class="static" :class="{'active':isActive,'text_danger':hasError}"></div>

    </div>

    <!-- <p id="info"></p> -->

    <script>
        const app={
            data(){
                return{
                    // counter:1
                    // kilometers:0,
                    // meters:0
                    isActive:false,
                    hasError:true,
                }
            }
        }
        //     watch:{
        //         meters:function(newValue,oldValue){
        //             if(this.currentlyActiveField==='meters'){
        //                 this.kilometers=newValue/1000;
        //                 this.meters=newValue;
        //             }
        //         },
        //         kilometers:function(newValue,oldValue){
        //             if(this.currentlyActiveField==='kilometers'){
        //                 this.kilometers=newValue;
        //                 this.meters=newValue*1000;
        //             }
        //         }
        //     }
        // }

        Vue.createApp(app).mount('#app')
        // vm.$watch('kilometers',function(a,b){
        //     document.getElementById("info").innerHTML='数字变化,修改前的值是:'+b+'修改后的值是'+a+'!!!!!!'
        // })
    </script>
</body>
</html>

当同时设置为TRUE的时候,后面的红色也会遮盖前面的颜色

这和前面写css的顺序有关,如果同时设置为TRUE,后写的css会遮盖先写的css属性

像这样,就会显示为蓝色

<!--
 * @Author: RealRoad1083425287@qq.com
 * @Date: 2023-04-02 19:41:53
 * @LastEditors: Mei
 * @LastEditTime: 2023-04-03 15:12:02
 * @FilePath: \vscode\Vue3_listen.html
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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>
    <script src="vue_doc/vue.global3.js"></script>
    <style>
    .text_danger{
        background: red;
    }
        .active{
        /* width:100px;
        height:100px; */
        background:blue;
    }
    .static{
        width: 100px;
        height:100px
    }
    
    </style>
   
</head>
<body>
    <div id="app">
        <!-- <p style = "font-size: 25px;">计数器:{{counter}}</p>
        <button @click="counter++" style="font-size: 25px;">点我</button> -->
        <!-- 千米:<input type="text" v-model="kilometers" @focus="currentlyActiveField='kilometers'">
        米:<input type="text" v-model="meters" @focus="currentlyActiveField='meters'"> -->
        <div class="static" :class="{'text_danger':hasError,'active':isActive,}"></div>

    </div>

    <!-- <p id="info"></p> -->

    <script>
        const app={
            data(){
                return{
                    // counter:1
                    // kilometers:0,
                    // meters:0
                    isActive:true,
                    hasError:true,
                }
            }
        }
        //     watch:{
        //         meters:function(newValue,oldValue){
        //             if(this.currentlyActiveField==='meters'){
        //                 this.kilometers=newValue/1000;
        //                 this.meters=newValue;
        //             }
        //         },
        //         kilometers:function(newValue,oldValue){
        //             if(this.currentlyActiveField==='kilometers'){
        //                 this.kilometers=newValue;
        //                 this.meters=newValue*1000;
        //             }
        //         }
        //     }
        // }

        Vue.createApp(app).mount('#app')
        // vm.$watch('kilometers',function(a,b){
        //     document.getElementById("info").innerHTML='数字变化,修改前的值是:'+b+'修改后的值是'+a+'!!!!!!'
        // })
    </script>
</body>
</html>

 

 

 当isActive或者hasError变化时,class属性值也将相应的更新。例如,如果active的值为TRUE,class列表将变为“static active text-danger”。我们也可以直接绑定数据里的一个对象:

同时使用多个类,并存在多个相同属性时,不管是class,还是:class,都会显示最后那个重复属性的定义。

 

 此外,我们也可以在这里绑定一个返回对象的计算属性。这是一个常用且强大的模式:

<!--
 * @Author: RealRoad1083425287@qq.com
 * @Date: 2023-04-02 19:41:53
 * @LastEditors: Mei
 * @LastEditTime: 2023-04-03 15:25:38
 * @FilePath: \vscode\Vue3_listen.html
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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>
    <script src="vue_doc/vue.global3.js"></script>
    <style>
    
        .active{
        /* width:100px;
        height:100px; */
        background:blue;
    }
    .static{
        width: 100px;
        height:100px;
        background:purple
    }
    .text_danger{
        background: red;
    }
    
    </style>
   
</head>
<body>
    <div id="app">
        <!-- <p style = "font-size: 25px;">计数器:{{counter}}</p>
        <button @click="counter++" style="font-size: 25px;">点我</button> -->
        <!-- 千米:<input type="text" v-model="kilometers" @focus="currentlyActiveField='kilometers'">
        米:<input type="text" v-model="meters" @focus="currentlyActiveField='meters'"> -->
        <div class="static" :class="classObject"></div>

    </div>

    <!-- <p id="info"></p> -->

    <script>
        const app={
            data(){
                return{
                    // counter:1
                    // kilometers:0,
                    // meters:0
                    isActive:true,
                    hasError:true,
                    error:null
                }
            },
            computed:{
                classObject(){
                    return{
                        active:this.isActive && !this.error,
                        'text-danger':this.error&&this.error.type==='fatal'
                    }
                }
            }
        }
        //     watch:{
        //         meters:function(newValue,oldValue){
        //             if(this.currentlyActiveField==='meters'){
        //                 this.kilometers=newValue/1000;
        //                 this.meters=newValue;
        //             }
        //         },
        //         kilometers:function(newValue,oldValue){
        //             if(this.currentlyActiveField==='kilometers'){
        //                 this.kilometers=newValue;
        //                 this.meters=newValue*1000;
        //             }
        //         }
        //     }
        // }

        Vue.createApp(app).mount('#app')
        // vm.$watch('kilometers',function(a,b){
        //     document.getElementById("info").innerHTML='数字变化,修改前的值是:'+b+'修改后的值是'+a+'!!!!!!'
        // })
    </script>
</body>
</html>

 数组语法

我们可以把一个数组传给v-bind:class。

<!--
 * @Author: RealRoad1083425287@qq.com
 * @Date: 2023-04-02 19:41:53
 * @LastEditors: Mei
 * @LastEditTime: 2023-04-03 15:30:13
 * @FilePath: \vscode\Vue3_listen.html
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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>
    <script src="vue_doc/vue.global3.js"></script>
    <style>
    
        .active{
        /* width:100px;
        height:100px; */
        background:blue;
    }
    .static{
        width: 100px;
        height:100px;
        background:purple
    }
    .text_danger{
        background: red;
    }
    
    </style>
   
</head>
<body>
    <div id="app">
        <!-- <p style = "font-size: 25px;">计数器:{{counter}}</p>
        <button @click="counter++" style="font-size: 25px;">点我</button> -->
        <!-- 千米:<input type="text" v-model="kilometers" @focus="currentlyActiveField='kilometers'">
        米:<input type="text" v-model="meters" @focus="currentlyActiveField='meters'"> -->
        <div class="static" :class="[activeClass,errorClass]"></div>

    </div>

    <!-- <p id="info"></p> -->

    <script>
        const app={
            data(){
                return{
                    // counter:1
                    // kilometers:0,
                    // meters:0
                    isActive:true,
                    hasError:true,
                    error:null,
                    activeClass:'active',
                    errorClass:'text_danger'
                }
            },
            computed:{
                classObject(){
                    return{
                        active:this.isActive && !this.error,
                        'text-danger':this.error&&this.error.type==='fatal'
                    }
                }
            }
        }
        //     watch:{
        //         meters:function(newValue,oldValue){
        //             if(this.currentlyActiveField==='meters'){
        //                 this.kilometers=newValue/1000;
        //                 this.meters=newValue;
        //             }
        //         },
        //         kilometers:function(newValue,oldValue){
        //             if(this.currentlyActiveField==='kilometers'){
        //                 this.kilometers=newValue;
        //                 this.meters=newValue*1000;
        //             }
        //         }
        //     }
        // }

        Vue.createApp(app).mount('#app')
        // vm.$watch('kilometers',function(a,b){
        //     document.getElementById("info").innerHTML='数字变化,修改前的值是:'+b+'修改后的值是'+a+'!!!!!!'
        // })
    </script>
</body>
</html>

 我们还可以使用三元表达式来切换列表中的class:

同样的例子,不一样的配方,继续搞起

<!--
 * @Author: RealRoad1083425287@qq.com
 * @Date: 2023-04-02 19:41:53
 * @LastEditors: Mei
 * @LastEditTime: 2023-04-03 15:35:10
 * @FilePath: \vscode\Vue3_listen.html
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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>
    <script src="vue_doc/vue.global3.js"></script>
    <style>
    .static{
        width: 100px;
        height:100px;
        background:purple
    }
    .active{
        /* width:100px;
        height:100px; */
        background:blue;
    }
    
    .text_danger{
        background: red;
    }
    
    </style>
   
</head>
<body>
    <div id="app">
        <!-- <p style = "font-size: 25px;">计数器:{{counter}}</p>
        <button @click="counter++" style="font-size: 25px;">点我</button> -->
        <!-- 千米:<input type="text" v-model="kilometers" @focus="currentlyActiveField='kilometers'">
        米:<input type="text" v-model="meters" @focus="currentlyActiveField='meters'"> -->
        <div class="static" :class="[isActive ? activeClass:errorClass]"></div>

    </div>

    <!-- <p id="info"></p> -->

    <script>
        const app={
            data(){
                return{
                    // counter:1
                    // kilometers:0,
                    // meters:0
                    isActive:true,
                    hasError:true,
                    error:null,
                    activeClass:'active',
                    errorClass:'text_danger'
                }
            },
            computed:{
                classObject(){
                    return{
                        active:this.isActive && !this.error,
                        'text-danger':this.error&&this.error.type==='fatal'
                    }
                }
            }
        }
        //     watch:{
        //         meters:function(newValue,oldValue){
        //             if(this.currentlyActiveField==='meters'){
        //                 this.kilometers=newValue/1000;
        //                 this.meters=newValue;
        //             }
        //         },
        //         kilometers:function(newValue,oldValue){
        //             if(this.currentlyActiveField==='kilometers'){
        //                 this.kilometers=newValue;
        //                 this.meters=newValue*1000;
        //             }
        //         }
        //     }
        // }

        Vue.createApp(app).mount('#app')
        // vm.$watch('kilometers',function(a,b){
        //     document.getElementById("info").innerHTML='数字变化,修改前的值是:'+b+'修改后的值是'+a+'!!!!!!'
        // })
    </script>
</body>
</html>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mez_Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值