5vue学习_动态绑定属性v-bind

本文介绍了Vue.js中使用v-bind进行动态属性绑定的方法,包括基本用法、类绑定、按钮事件处理及数组方法调用等高级技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、对属性的动态绑定,如超链接,图像等众多地方

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title-hellovuejs</title>
</head>
<body>
    <div id ="app">
        <h2>{{message}}</h2>
        <img v-bind:src="imgUrl" alt="">
        <p>  
        <a v-bind:href="URL">百度一下</a>    
    </div>
    <script src="../js/vue.js"></script>
    <script>
        //let(变量) /const(常亮)
        const chen1 = new Vue({
            el: '#app',  //用于挂载要管理的元素
            data:{//定义数据
                message: '绑定测试', 
                imgUrl: 'https://www.baidu.com/favicon.ico',
                URL: 'https://www.baidu.com' 
            },
        })
    </script>
</body>

效果如下

2、v-bind的缩写,语法糖

        <img :src="imgUrl" alt="">
        <p>  
        <a :href="URL">百度一下</a>  

整个程序可以简化为

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title-hellovuejs</title>
</head>
<body>
    <div id ="app">
        <h2>{{message}}</h2>
        <!-- <img v-bind:src="imgUrl" alt="">
        <p>  
        <a v-bind:href="URL">百度一下</a>     -->
<!-- 语法糖,v-bind的缩写为  :  -->
        <img :src="imgUrl" alt="">
        <p>  
        <a :href="URL">百度一下</a>    
    </div>
    <script src="../js/vue.js"></script>
    <script>
        //let(变量) /const(常亮)
        const chen1 = new Vue({
            el: '#app',  //用于挂载要管理的元素
            data:{//定义数据
                message: '绑定测试', 
                imgUrl: 'https://www.baidu.com/favicon.ico',
                URL: 'https://www.baidu.com' 
            },
        })
    </script>
</body>

3、对类的绑定!!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title-hellovuejs</title>
    <style>
        .active{
            color:red;
        }
    </style>
</head>
<body>
    <div id ="app">
        <!-- <h2 v-bind:class="{类名1:boolean,类名2:boolean}">{{message}}</h2>
        <h2 v-bind:class="{类名1:true,类名2:false}">{{message}}</h2> -->
        <!-- boolean为true类自动绑定在上面 -->
        <h2 v-bind:class="{active:isActive,Line:isLine}">{{message}}</h2>
    </div>
    <script src="../js/vue.js"></script>
    <script>
        //let(变量) /const(常亮)
        const chen1 = new Vue({
            el: '#app',  //用于挂载要管理的元素
            data:{//定义数据
                message: '绑定测试', 
                isActive: true,
                isLine: true
            },
        })
    </script>
</body>

结果交互如下

4、按钮操作例子

点击按钮后,颜色来回变换

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title-hellovuejs</title>
    <style>
        .active{
            color:red;
        }
    </style>
</head>
<body>
    <div id ="app">
        <!-- <h2 v-bind:class="{类名1:boolean,类名2:boolean}">{{message}}</h2>
        <h2 v-bind:class="{类名1:true,类名2:false}">{{message}}</h2> -->
        <!-- boolean为true类自动绑定在上面 -->
        <h2 v-bind:class="{active:isActive,Line:isLine}">{{message}}</h2>
        <button v-on:click="chenbtClick">按钮</button>
    </div>
    <script src="../js/vue.js"></script>
    <script>
        //let(变量) /const(常亮)
        const chen1 = new Vue({
            el: '#app',  //用于挂载要管理的元素
            data:{//定义数据
                message: '绑定测试', 
                isActive: true,
                isLine: true
            },
            methods:{
                chenbtClick: function(){
                    this.isActive = !this.isActive;
                }
            }
        })
    </script>
</body>

5、简化为方法操作,按钮操作例子

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title-hellovuejs</title>
    <style>
        .active{
            color:red;
        }
    </style>
</head>
<body>
    <div id ="app">
        <!-- <h2 v-bind:class="{类名1:boolean,类名2:boolean}">{{message}}</h2>
        <h2 v-bind:class="{类名1:true,类名2:false}">{{message}}</h2> -->
        <!-- boolean为true类自动绑定在上面 -->
        <!-- <h2 v-bind:class="{active:isActive,Line:isLine}">{{message}}</h2> -->
        <h2 :class="getClasses()">{{message}}</h2>
        <button v-on:click="chenbtClick">按钮</button>
    </div>
    <script src="../js/vue.js"></script>
    <script>
        //let(变量) /const(常亮)
        const chen1 = new Vue({
            el: '#app',  //用于挂载要管理的元素
            data:{//定义数据
                message: '绑定测试', 
                isActive: true,
                isLine: true
            },
            methods:{
                chenbtClick: function(){
                    this.isActive = !this.isActive;
                },
                getClasses: function(){
                    return {active:this.isActive,Line:this.isLine};
                }
            }
        })
    </script>
</body>

6、基于数组的方法调用类

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title-hellovuejs</title>
    <style>
        .active{
            color:red;
        }
    </style>
</head>
<body>
    <div id ="app">
        <!-- <h2 v-bind:class="{类名1:boolean,类名2:boolean}">{{message}}</h2>
        <h2 v-bind:class="{类名1:true,类名2:false}">{{message}}</h2> -->
        <!-- boolean为true类自动绑定在上面 -->
        <!-- <h2 v-bind:class="{active:isActive,Line:isLine}">{{message}}</h2> -->
        <h2 class="otherclass" :class="[active1,line1]">{{message}}</h2>
        <h2 class="otherclass" :class="getClasses()">{{message}}</h2>

        <button v-on:click="chenbtClick">按钮</button>
    </div>
    <script src="../js/vue.js"></script>
    <script>
        //let(变量) /const(常亮)
        const chen1 = new Vue({
            el: '#app',  //用于挂载要管理的元素
            data:{//定义数据
                message: '绑定测试', 
                isActive: true,
                isLine: true,
                active1: 'aaaaa',
                line1: 'bbbbb'

            },
            methods:{
                chenbtClick: function(){
                    this.isActive = !this.isActive;
                },
                getClasses: function(){
                    //return {active:this.isActive,Line:this.isLine};
                    return [this.active1,this.line1];
                }
            }
        })
    </script>
</body>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值