vue初识之事件修饰符及样式的使用


一、事件修饰符

1. .stop

阻止冒泡

<header @click="head">
            <aside @click="asdie">
                <button @click.stop="btn">点击</button>
            </aside>
        </header>
let vm = new Vue({
        el: "#app",
        data: {

        },
        methods: {
            head() {
                console.log(3);
            },

            asdie() {
                console.log(2);
            },
            btn() {
                console.log(1);
            },
            targer() {
                console.log('点击了');
            }
        }
    })

2. .capture

添加事件侦听器时使用事件捕获模式

<header @click="head">
            <aside @click.capture="asdie">
                <button @click="btn">点击</button>
            </aside>
        </header>

3. .self

只当事件在该元素本身(比如不是子元素)触发时触发回调

<header @click="head">
            <aside @click.self="asdie">
                <button @click="btn">点击</button>
            </aside>
        </header>

4. .once

事件只触发一次

<header @click="head">
            <aside @click="asdie">
                <button @click.once="btn">点击</button>
            </aside>
        </header>

5. .prevent

阻止默认事件

<a @click.prevent="targer" href="https://www.baidu.com/">百度一下</a>

二、样式的使用

1.数组

<div id='app'>
        <div class="box red">今天下雨了</div>

        <div :class="class1">今天下雨了</div>
        <!-- 数组 -->
        <div :class="class2">今天下雨了</div>
        
    </div>
 const vm = new Vue({
            el: '#app',
            data: {
                flag: false,
                class1: 'box red',
                class2: ['box', 'red'],
                class3: [{ 'box': true, 'fs40': true }]
            },
            methods: {
                change() {
                    // this.class1 += ' fs40'
                    this.class2.push('fs40')
                }
            }
        })

2.三木表达式

<!-- 三木表达式 -->
        <div :class="flag?'red':'fs40'">今天下雨了</div>
        

3.数组内置对象(对象的键是样式的名字,值是bool类型)

<!-- 数组内置对象(对象的键是样式的名字,值是bool类型) -->
        <div :class="class3">今天下雨了</div>
        


        <button @click="change">change</button>

4.直接通过对象

<!-- 	直接通过对象 -->
        <div :class="{'box':true}">今天下雨了</div>

总结

以上就是今天要讲的内容,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值