• 使用vuex实时更新右上角通知信息的红点数量


    一· 通过事件绑定实现

    1. 定义全局事件触发器 event-emitter.js

    1. /* 事件触发器 */
    2. class EventEmitter {
    3. constructor() {
    4. this.subs = Object.create(null) // 对象没有 原型属性 提高性能
    5. }
    6. // 注册事件
    7. $on(eventType, handler) {
    8. this.subs[eventType] = this.subs[eventType] || []
    9. this.subs[eventType].push(handler)
    10. }
    11. //触发事件
    12. $emit(eventType) {
    13. if (this.subs[eventType]) {
    14. this.subs[eventType].forEach(handler => {
    15. handler()
    16. })
    17. }
    18. }
    19. }
    20. const em = new EventEmitter()
    21. export default em

    2. 引用事件触发器  main.js

    1. import EventEmitter from '@/utils/event-emitter'
    2. /* 全局事件 */
    3. Vue.prototype.$eventEmitter = EventEmitter

    3. 在需要的地方进行事件 绑定

    1. // 加载的时候进行绑定
    2. mounted() {
    3. /* 绑定 刷新待办信息 */
    4. //this.$eventEmitter.$on('refreshCheckwait', this.getCheckwait)
    5. /* 绑定 notice 列表 */
    6. this.$eventEmitter.$on('refreshNotice', this.getNewMessage)
    7. },
    8. // 声明方法, 供绑定的事件调用
    9. methods: {
    10. // 获取最新消息
    11. getNewMessage(){
    12. messageApi.getList({ ...this.messageSearchption }).then(res=>{
    13. const data = res.data.data
    14. if(data) this.noReadNum = data.noReadNum
    15. }).catch(err=>{console.log(err);})
    16. },
    17. }

    4. 调用: 全局绑定事件 和 使用

    1. // 请求后台接口后,刷新绑定的全局事件,调用方法。进行数据更新
    2. checkApi.save(params).then(res => {
    3. const data = res.data
    4. if(data.success) {
    5. this.$notify({
    6. title: '成功',
    7. message: '提交成功',
    8. type: 'success'
    9. })
    10. this.$eventEmitter.$emit('refreshCheckwait')
    11. this.$router.push({name: 'qjapproval-page'})
    12. } else {
    13. this.$notify({
    14. title: '失败',
    15. message: '提交失败',
    16. type: 'error'
    17. })
    18. }
    19. this.submitLoading = false
    20. }).catch(err => this.submitLoading = false)

    2.通过 Vuex 全局状态存储实现 如下:

    需求如图:因为这两个不存在组件关系,所以我们使用Vuex来解决这个实时刷新

    1.首先在vuex的state定义数据如下

    1. state{
    2. noticeCount: 0,
    3. }

    2.更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数,在通过mutation更新数据的时候, 有可能我们希望携带一些额外的参数,你可以向 store.commit 传入额外的参数

    1. //noticeCount额外的参数
    2. SET_NOTICE_COUNT: (state, noticeCount) => {
    3. console.log("noticeCount", noticeCount);
    4. state.noticeCount = noticeCount;
    5. },

    3.因为我们的接口请求数据只能放在action异步请求这里,为何把接口数据放在vuex呢?因为你可能这个铃铛数量的改变是多个页面影响的。

    1. actions: {
    2. getUnreadMessagesCount({ commit }) {
    3. inventoryNotice.getInventoryCount().then((res) => {
    4. //异步获取到铃铛的数量, 触发mutations的方法 把值赋给noticeCount
    5. commit("SET_NOTICE_COUNT", res.data.data);
    6. });
    7. },
    8. },

    4.接下来vuex的工作都准备好了,接下来在你需要操作的页面点击按钮事件那里调用action的方法

     this.$store.dispatch("getUnreadMessagesCount");//调用vuex里面的异步方法 请求获取到铃铛的数量

    5.最后在铃铛页面使用监听属性(computed)拿到数据

    1. import { mapState } from "vuex";
    2. computed:{
    3. ...mapState({
    4. count: (state) => state.common.noticeCount, //直接拿到count数量
    5. })
    6. }

    至此利用vuex进行实时刷新的消息通知数量的功能已完成。有疑问的小伙伴可以多交流沟通

  • 相关阅读:
    批量差异分析 批量findmarkers
    idea项目一直在build
    关于Git分支基础知识的一些笔记
    postgresql源码学习(十五)—— 行锁③-死锁检测
    小程序开发音视频问题汇总及解决方案
    torch 写模型遇到 RuntimeError: Found dtype Double but expected Float
    Werewolf puzzle Privacy Policy
    2022英伟达显卡排名天梯图
    l14 IO模型
    静态网卡配置centos、kali、ubantu
  • 原文地址:https://blog.csdn.net/weixin_39255905/article/details/125424463