Vue v-bind v-on

Vue v-bind

基本使用
 <!-- vue3允许 template 中有多个根元素 -->
    <template id="tem">
        <!-- v-bind 基本使用 -->
        <img v-bind:src="imgUrl" alt="">
        <a v-bind:href="link">baidu </a>

        <!-- 语法糖 简写 -->
        <img :src="imgUrl" alt="">
        <img src="imgUrl" alt="">
        <a :href="link">baidu </a>
    </template>
     data(){
            return{ 
                imgUrl:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2F1812.img.pp.sohu.com.cn%2Fimages%2Fblog%2F2009%2F11%2F18%2F18%2F8%2F125b6560a6ag214.jpg&refer=http%3A%2F%2F1812.img.pp.sohu.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623658315&t=17d8bb35a4d79b34ce4052f0cd9b5c4b',
                link:'https://www.baidu.com/'
            }
        },
绑定class
class对象
<template id="tem">
        <div :class="className"> hahah </div>
        <!-- 可以多个键值对 -->
        <div :class="{active: isActive , 'title':true}">哈哈哈哈</div>
        <!-- 默认class 和动态想结合 -->
        <div class="abc cba" :class="{active: isActive , 'title':true}">哈哈哈哈</div>
        <button @click='tog'>change</button>

        <!-- 将对象放到一个单独的属性中 -->
        <div class="abc cba" :class="obj123">hh123</div>
        <!-- 将返回的对象放到一个methods方法中 -->
        <div class="abc cba" :class="getobj1()">hh123</div>

    </template>
    data(){
            return{ 
                className:"why",
                isActive:true,
                title:'abc',
                obj123:{
                    active: true,
                    title: true
                }
            }
        },
        methods:{
            tog(){
                this.isActive=!this.isActive
            },
            getobj1(){
                return {
                    active: true,
                    title: true
                }
            }
        }
class数组
<template id="tem">
        <div :class="['abc',title]">hello111</div>
        <div :class="['abc',title, isAcitve?'active':'' ]">hello111</div>
        <div :class="['abc',title, {active:isAcitve}]">hello111</div>
    </template>
绑定style对象
<template id="tem">
        <!-- :style="{cssPropertyName:cssPrpperValue}" -->
        <div :style="{color: finalColor,'font-size':'30px'}">哈哈哈哈</div>
        <div :style="{color: finalColor,fontSize:'30px'}">哈哈哈哈</div>
        <div :style="{color: finalColor,'font-size': finalFontSize +'px'}">哈哈哈哈</div>
        
        <!-- 绑定data中属性 -->
        <div :style="finalStyle">hhhh</div>
        <!-- 调用方法 -->
        <div :style="getfinalStyle()">hhhh</div>
    </template>


data(){
            return{ 
               finalColor:'red',
               finalFontSize:50,
               finalStyle:{
                   'font-size':'50px',
                   fontWeight:700,
                   backgroundColor:'red'
               }
            }
        },
        methods:{
            getfinalStyle(){
                return {
                    'font-size':'50px',
                   fontWeight:700,
                   backgroundColor:'red'
                }
            }
        }
绑定style数组语法
 <div :style="[styleObj1,styleObj2]">hello111</div> 
 data(){
            return{ 
                styleObj1:{
                    color:'red',
                    fontSize:'50px',
                },
                styleObj2:{
                    textDecoration:'underline'
                }
            }
        },
动态绑定属性名称
 <div :[name]="value">
            哈哈哈
        </div>
       data(){
            return{ 
                name:'cba',
                value:'tom'
            }
        },  
直接绑定一个对象
<div v-bind="per">
            hahah 
 </div>
 <div :="per">123</div>
        return{ 
               per:{
                   name:'tom',
                   age:18,
                   height:180
               }
            }

在这里插入图片描述

v-on
v-on基本使用
<!-- v-on:监听事件="methods中的方法" -->
            <button v-on:click="btnclick">1</button>
            <div v-on:mousemove="mousemove">2</div>
            <!-- 语法糖 @监听事件="methods中的方法" -->
            <button @click="btnclick">1</button>
            <div @mousemove="mousemove">2</div>

            <!-- 绑定一个表达式 inline statement -->
            <button @click="count++">{{count}}</button>

            <!-- 绑定一个对象 -->
            <div @="{ click:btnclick,mousemove:mousemove}">
                666
            </div>
            <div v-on="{ click:btnclick,mousemove:mousemove}">
                666
            </div>
            data(){
            return{ 
                count:100
            }
        },
        methods:{
            btnclick(){
                alert('1')
            },
            mousemove(){
                console.log('123');
            },
        }
v-on参数传递
<button @click="btnclick('tom')">点击</button>
 btnclick(name){
                // 默认传入event
                console.log(event);
                console.log(name);
            }

在这里插入图片描述

v-on修饰符 阻止冒泡 enter监听
<div @click="divclick">
            <button @click.stop="btnclick">按钮</button> 
            <!-- 阻止冒泡 -->
        </div>
        <!-- 监听键盘 -->
        <input type="text " @keyup="keye">
        <!-- 只监听 enter -->
        <input type="text " @keyup.enter="keye">
methods:{
            divclick(){
                console.log('div');
            },
            btnclick(){
                console.log('btn');
            },
            keye(){
                console.log(event.target.value);
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值