码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • vue3项目中mitt的使用


    • Vue2.x使用EventBus进行组件通信,而Vue3.x推荐使用mitt.js。
    • 比起Vue实例上的EventBus,mitt.js好在哪里呢?首先它足够小,仅有200bytes,其次支持全部事件的监听和批量移除,它还不依赖Vue实例,所以可以跨框架使用,React或者Vue,甚至jQuery项目都能使用同一套库。

    安装

    npm install --save mitt
    
    • 1

    创建Mitt实例

    utils/eventBus.ts

    import mitt from 'mitt'
    
    export const globalEventBus = mitt()
    
    • 1
    • 2
    • 3

    使用

    // 引入
    import { globalEventBus } from '@/utils/eventBus'
    
    // emit
    setInterval(() => {
      globalEventBus.emit('test', 1)
    }, 2000)
    
    const fn = (val) => {
      console.log(val)
      console.log('fn')
    }
    
    // on
    globalEventBus.on('test', fn)
    
    // off
    onBeforeUnmount(() => {
      console.log('onBeforeUnmount')
      globalEventBus.off('test') // 生效
      // globalEventBus.off('test', fn) // 生效,但fn内的代码不会执行
      // globalEventBus.off('test', () => { // 不生效
      //   console.log('off')
      // })
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    注意:

    • 一定要手动关闭全局监听事件,off
    • 如上所示,off生效与不生效的情况一定要注意!!off重新定义一个回调函数是不会生效的,传入订阅时的回调函数才会生效,但是回调函数里面的代码不执行。

    详细使用

    参考官网

    import mitt from 'mitt'
    
    const emitter = mitt()
    
    // listen to an event
    emitter.on('foo', e => console.log('foo', e) )
    
    // listen to all events
    emitter.on('*', (type, e) => console.log(type, e) )
    
    // fire an event
    emitter.emit('foo', { a: 'b' })
    
    // clearing all events
    emitter.all.clear()
    
    // working with handler references:
    function onFoo() {}
    emitter.on('foo', onFoo)   // listen
    emitter.off('foo', onFoo)  // unlisten
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    RabbitMQ--基础--04--运转流程
    竞赛 题目:垃圾邮件(短信)分类 算法实现 机器学习 深度学习 开题
    html css制作正六边形
    用CMake编译项目 & CMake和g++的区别
    工具及方法 - 使用Total Commander来查找重名文件
    Qt源码解读(一)Windows消息循环和事件循环机制
    穿越时空的创新:解析云原生与Web3.0的奇妙渊源
    hive笔记(四):查询、分组-运算符/limit/where/like/rlike/group by/having
    中国市场杂志社中国市场编辑部2022年第32期目录
    Linux学习——进程间通信
  • 原文地址:https://blog.csdn.net/Anyuegogogo/article/details/133578082
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号