• 第六节——Vue中的事件


    一、定义事件

    Vue 元素的事件处理和 DOM 元素的很相似,但是有一点语法上的不同

    使用@修饰符(v-on:的缩写)+事件名的方式 给dom添加事件后面跟方法名,方法名可以直接加括号如@click="add()"里面进行传参。对应的事件处理函数必须在 methods对象中定义。

    1. <template>
    2. <div>
    3. <!-- 在button上定义点击事件 -->
    4. <button @click="hello('传入的参数')">你好</button>
    5. </div>
    6. </template>
    7. <script>
    8. export default {
    9. /**
    10. * methods 在vue定义 方法的属性对象
    11. * 所有的方法都必须在methods里面定义
    12. */
    13. methods: {
    14. hello (msg) {
    15. console.log("事件触发啦哈哈哈")
    16. console.log(msg)
    17. }
    18. }
    19. }
    20. </script>

    二、事件修饰符

    为了更好地处理事件,Vue3提供了一些便利的事件修饰符。事件修饰符可以用于改变默认事件行为、限制事件触发条件等如.stop、.prevent、.capture、.self、.once等等。下面是一些常用的事件修饰符用法

    1、.stop

    阻止事件冒泡,即停止事件在父元素中的传播。

    1. <template>
    2. <div class="box" @click="handle2">
    3. <div class="box2" @click="handle"></div>
    4. </div>
    5. </template>
    6. <script>
    7. export default {
    8. methods: {
    9. handle () {
    10. console.log('触发')
    11. },
    12. handle2 () {
    13. console.log("冒泡")
    14. }
    15. }
    16. }
    17. </script>

    2、.prevent

    阻止事件的默认行为,如提交表单或点击链接后的页面跳转。

    1. <template>
    2. <a href="https://www.baidu.com" @click.prevent="handle">百度a>
    3. template>
    4. <script>
    5. export default {
    6. methods: {
    7. handle() {
    8. console.log("触发");
    9. }
    10. },
    11. };
    12. script>

    3、.once

    只触发一次事件处理方法,之后解绑事件

    1. <template>
    2. <button @click.once="handle">点击一次就失效</button>
    3. </template>
    4. <script>
    5. export default {
    6. methods: {
    7. handle() {
    8. console.log("触发");
    9. },
    10. },
    11. };
    12. </script>

    三、event对象

    1、默认传入获取event

    1. <template>
    2. <button @click="handle">点击button>
    3. template>
    4. <script>
    5. export default {
    6. methods: {
    7. handle(event) {
    8. console.log(event);
    9. },
    10. },
    11. };
    12. script>

    2、携带其他参数获取event

    1. <template>
    2. <button @click="handle('第一个参数', $event)">点击button>
    3. template>
    4. <script>
    5. export default {
    6. methods: {
    7. handle(msg, event) {
    8. console.log(event);
    9. },
    10. },
    11. };
    12. script>

    四、在函数内使用this获取当前Vue上下文

    可以直接使用this.xx 使用data里定义的状态,或者使用this.xx()调用methods里面定义的其他函数

    注意:this指向问题

    1. <template>
    2. <button @click="handle">点击</button>
    3. </template>
    4. <script>
    5. export default {
    6. data() {
    7. return {
    8. num: 1,
    9. };
    10. },
    11. methods: {
    12. handle() {
    13. console.log(this.num);
    14. this.handle2()
    15. },
    16. handle2() {
    17. console.log("第二个方法");
    18. },
    19. },
    20. };
    21. </script>
  • 相关阅读:
    Scrum敏捷模式的优势点、实践经验及适用企业
    算法练习15——加油站
    C++面向对象
    journal/rsyslog日志丢失问题解决
    C# | .NET命名规范
    OpenAI 超 700 名员工联名逼宫董事会;ChatGPT 新功能“阅后即焚”丨 RTE 开发者日报 Vol.89
    20分钟---Vue2->Vue3
    购买阿里云服务器需要多少钱?活动价2000元-3000元的阿里云服务器汇总
    Lua Table(表)
    想要制作照片书的看这里!
  • 原文地址:https://blog.csdn.net/weixin_57017198/article/details/134063796