• 第六节——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>
  • 相关阅读:
    盘点渗透测试常用工具,新手收藏少走弯路
    java 实现字典树(Trie)
    【Selenium】WebDriverPool让动态爬虫变得更简单高效稳定
    47个Docker常见故障的原因和解决方式
    MySQL数据库描述以及安装使用
    0-JavaWeb基础总结
    构建房地产行业智慧采购新模式,采购协同商城系统护航企业采购数字化转型
    Linux【搭建环境与基本指令】
    Docker常用命令
    网页的盒子模型是什么?怎么设置盒子样式
  • 原文地址:https://blog.csdn.net/weixin_57017198/article/details/134063796