element-ui 全局封装消息提示、Notification通知、确认提示框等等~

这篇博客介绍了如何在Vue.js项目中创建一个名为mixBox的模块,该模块包含了messageBox、notifyBox和confirmMessageBox等方法,用于实现消息提示、通知和确认操作。同时,还提供了exportExcelBox方法用于表格数据的Excel导出。在main.js中引入并使用这些功能,需要安装file-saver和xlsx库。博客提供了具体的代码示例和使用方法。
摘要由CSDN通过智能技术生成

src中 新建mixBox文件夹

mixBox文件夹下建 index.js 和 message.js文件

1. index.js 内容

export { default as messageBox } from './message'

2.mixBox.js内容

export default{

     

    methods:{

       

      // 消息提示

      messageBox(message,type,userHTML,showClose,duration){

       

        const msgInfo = message

        const msgType = type || 'warning' //默认是warning(警告)    message(信息) success(成功) error(错误)

        const msgUserHTML = userHTML || false

        const msgShowClose = showClose || false

        const msgDuration = duration || 2000  //时间可以适当调整

        this.$message({

          message: msgInfo,

          type:msgType,

          dangerouslyUseHTMLString:msgUserHTML,

          showClose:msgShowClose,

          duration:msgDuration

        })

      },



      //Notification通知

      notifyBox(title,message,type,userHTML,showClose,duration){

       

        const msgInfo = message

        const titleInfo = title

        const msgType = type || 'warning' //默认是warning(警告)    message(信息) success(成功) error(错误)

        const msgUserHTML = userHTML || false

        const msgShowClose = showClose || false

        const msgDuration = duration || 2000  //时间可以适当调整

        this.$notify({

          message: msgInfo,

          title: titleInfo,

          type:msgType,

          dangerouslyUseHTMLString:msgUserHTML,

          showClose:msgShowClose,

          duration:msgDuration

        })

      },

     

      // 确认提示框

      confirmMessageBox(message,title, confirType, msgUserHTML){

       

        return new Promise((resolve,reject)=>{

       

          this.$confirm(message, title || '提示',{

       

            confirmButtonText:'确认',

            cancelButtonText:'取消',

            dangerouslyUseHTMLString:msgUserHTML || false,

            type:confirType || 'warning' //默认是warning 警告

          }).then(()=>{

       

            resolve()

          }).catch(()=>{

               

          })

        })

      },



      //当前时间

      getTiemdata(tieM){

          var boxDate = null

          if( tieM !== undefined){

              boxDate = tieM

          }else{

              boxDate = new Date();

          }

           let d = new Date(boxDate);

          let year = d.getFullYear()

          let month = (d.getMonth() + 1) < 10 ? '0'+(d.getMonth() + 1) : (d.getMonth() + 1);

          let day = d.getDate()<10 ? '0'+d.getDate() : d.getDate();

          let hours = d.getHours()<10 ? '0'+d.getHours() : d.getHours();

          let min = d.getMinutes()<10 ? '0'+d.getMinutes() : d.getMinutes();

          let sec = d.getSeconds()<10 ? '0'+d.getSeconds() : d.getSeconds();

          const datatiem =  year + "-" + month + "-" + day + " " + hours + ":" + min + ":" + sec;

          // console.log('全局',datatiem)

          return datatiem

      },



      //导出

      exportExcelBox(id,fixed,fixed_right,name) {

        console.log(111)

        var wb = this.$XLSX.utils.table_to_book(document.querySelector(id),{raw:true});

        var fix = document.querySelector(fixed)

        var fixright = document.querySelector(fixed_right)

         if (fix) {

                wb = this.$XLSX.utils.table_to_book(document.querySelector(id).removeChild(fix),{raw:true});

                document.querySelector(id).appendChild(fix);

        }

        if (fixright) {

                wb = this.$XLSX.utils.table_to_book(document.querySelector(id).removeChild(fixright),{raw:true});

                document.querySelector(id).appendChild(fixright);

        }

        var wbout = this.$XLSX.write(wb, {

            bookType: "xlsx",

            bookSST: true,

            type: "array"

        });

        try {

            this.$FileSaver.saveAs(

            new Blob([wbout], { type: "application/octet-stream" }),

            //设置导出文件名称

            name + ".xlsx"

            );

        } catch (e) {

            if (typeof console !== "undefined") console.log(e, wbout);

        }

     },



    }



  }

  //使用方法=>直接在使用的页面调用 例子如下

   // this.messageBox("警告",'warning')

  //  this.notifyBox("成功",'添加成功','success')

   // this.confirmMessageBox('您确认要删除吗','提示','error').then(()=>{

   //       console.log('删除成功')

   // }

   // this.getTiemdata() //不传值时默认获取当前时间

//  this.exportExcelBox( "#self-table", ".el-table__fixed", ".el-table__fixed-right", "导出的名字")

  //注意#self-table是 => table的id

3.在main.js

//引入封装好的消息提示框、确认提示框和Notification通知等

import { messageBox } from "./mixBox/index"

Vue.mixin(messageBox)

//引入导出excel表格模板

import FileSaver from 'file-saver'

import XLSX from 'xlsx'

4. 导出excel需要安装

npm install -S file-saver xlsx

npm install -D script-loader

//仅供参考,如有雷同,纯属虚构

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

太空游走的鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值