vue+element项目中页面多个接口异常,只提示一次异常信息

有时候一个页面会同时调多个接口,但是多个接口异常,需要做提示,那么提示的时候会弹出很多的提示信息,这无疑让体验感降低很多。  所以针对这种情况,我们配合element UI统一做一个异常状态的处理,只能显示一次提示的功能,后续代码调接口的时候

也可以省略去写异常状态下的逻辑了。
首先新建一个文件 messageOnce.js,内容如下:

import {Message} from 'element-ui'
// 私有属性,只在当前文件可用
const showMessage = Symbol('showMessage')
export default class domMessage {
    success (options, single = true) {
        this[showMessage]('success', options, single)
    }
    warning(options, single = true) {
        this[showMessage]('warning', options, single)
    }
    info(options, single = true) {
        this[showMessage]('info', options, single)
    }
    error(options, single = true) {
        this[showMessage]('error', options, single)
    }
    [showMessage] (type, options, single) {
        if (single) {
            // 判断当前页是否有el-message标签,如果没有则执行弹窗操作
            if (document.getElementsByClassName('el-message').length === 0) {
                Message[type](options)
            }
        } else {
            Message[type](options)
        }
    }
}

第二步,需要在你的响应拦截器(interceptors.response.use)的页面去引入刚才的文件,我取名为domMessage,引入:import domMessage from './messageOnce'
第三步,new 对象实例 const messageOnce = new domMessage()
第四步,在响应拦截器中去写

if (response.data.code && JSON.parse(response.data.code) == 500) {
      messageOnce.warning({
        message: '系统异常'+response.data.msg,
        type: 'warning'
      })
}

然后尝试就行了。

相关推荐

  1. 日志被吞,异常类型,没有堆栈信息

    2023-12-17 06:30:03       34 阅读
  2. Qt线程槽函数无法触发异常排查

    2023-12-17 06:30:03       20 阅读
  3. flutter页面异常监测

    2023-12-17 06:30:03       12 阅读
  4. C++ 线程捕捉异常

    2023-12-17 06:30:03       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-17 06:30:03       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-17 06:30:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-17 06:30:03       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-17 06:30:03       20 阅读

热门阅读

  1. vue制作简易日历

    2023-12-17 06:30:03       36 阅读
  2. 计算机网络

    2023-12-17 06:30:03       36 阅读
  3. 计算机网络英文总结

    2023-12-17 06:30:03       38 阅读
  4. B+树和索引

    2023-12-17 06:30:03       33 阅读
  5. 前端传值及本地存储方式的简单介绍

    2023-12-17 06:30:03       35 阅读
  6. ES如何提高准确率之【term-centric】

    2023-12-17 06:30:03       38 阅读
  7. 使用docker实现logstash同步mysql到es

    2023-12-17 06:30:03       46 阅读
  8. Node.js

    Node.js

    2023-12-17 06:30:03      39 阅读
  9. Canvas学习笔记

    2023-12-17 06:30:03       44 阅读
  10. 第十六章 : Spring Boot JWT 集成redis实现分布式token

    2023-12-17 06:30:03       29 阅读