vue项目添加全局消息弹窗

文章展示了如何在Vue项目中利用ElementUI库创建一个全局对话框组件。组件包含了数据渲染,如表格展示、图片显示以及消息列表,同时提供了打开弹窗、查看更多功能。在main.js中进行了全局挂载,使得在项目任何地方都能调用此组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 前提:本弹窗使用的组件是element ui,需要在项目安装,具体安装请移步官网
  2. 创建全局组件,代码如下
<template>
  <div class="confirm">
    <el-dialog
      append-to-body lock-scroll
      ref="executeResult"
      title="全局弹窗"
      :visible.sync="visible"
      :width="hasMore?'50%':'30%'"
    >
      <div v-if="!hasMore">
        <div class="confirm-image">
          <el-image
            style="width: 100px; height: 100px"
            :src="url"
            fit="fit"></el-image>
          <div>{{ operateCount }}条数据,审核成功{{ successCount }}条,失败{{ cancelCount }}</div>
        </div>
        <div class="message-box">
          <div class="confirm-message" v-for="(item,index) in msgList.slice(0,5).reverse()" :key="index">
            {{ item }}
          </div>
        </div>
        <div class="message-more" v-if="msgList.length>5" @click="viewMore">查看更多</div>
      </div>
      <div v-else>
        <el-table border highlight-current-row :data="tableData" style="width: 100%" max-height="350">
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="status" label="状态"></el-table-column>
          <el-table-column prop="msg" label="处理结果"></el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="visible = false">取 消</el-button>
    <el-button type="primary" @click="visible = false">我知道了</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
const url = require('@/assets/images/icon/question.png')
export default {
  name: "index",
  data() {
    return {
      tableData: [],//表格数据
      visible: false,//控制弹窗
      url: url,
      cancelCount: 0,//失败条数
      operateCount: 0,//操作条数
      successCount: 0,//成功条数
      msgList: [],
      hasMore: false,//是否显示更多
    }
  },
  watch: {
    visible(newValue, oldValue) {
      if (!newValue) {
        // 当弹窗状态为隐藏时
        let confirm = document.getElementsByClassName('confirm');
        if (confirm.length >= 2) {
          // 如果页面中类名为confirmDel的元素个数大于等于2时,则删除第一个元素,确保页面中只有一个confirmDel元素
          let confirmDel = confirm[0];
          document.body.removeChild(confirmDel);
        }
      }
    }
  },
  methods: {
    //初始化弹窗
    openDialog() {
      this.visible = true
      this.$message.success('操作成功!')
    },
    //查看更多按钮
    viewMore() {
      this.tableData = this.msgList.reverse().map(item => {
        return {
          status: '错误',
          msg: item
        }
      })
      this.$nextTick(() => {
        this.hasMore = true
      })
    },
  }
}
</script>

<style scoped>
//这个CSS就是将dialog弹窗取消全屏显示,让他在某个div元素内部,这个背景相当于自己实现了遮罩,前提还需要把
//append-to-body;modal-append-to-body;modal设置为false即可
//.confirm {
  //::v-deep .el-dialog__wrapper {
    //position: absolute !important;
    //background: rgb(0, 0, 0, 0.4);
  //}
//}
.confirm-image {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.message-box {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;

  .confirm-message {
    padding: 10px;
  }
}

.message-more {
  color: #00afff;
  cursor: pointer;
}
</style>

  1. 在main.js中全局挂载该组件
//挂载执行结果组件
import executeResult from "@/components/ExecuteResult/index.vue";

const messageBox = Vue.extend(executeResult)
//此处options就是你给该组件传递的data值
executeResult.install = function (options) {
//使用$mount()给组件手动挂载参数
  let instance = new messageBox({
    data: options
  }).$mount()

  document.body.appendChild(instance.$el); //将组件插入页面中
  //注解:假如不想是全屏的,只是想将弹窗添加进入每个页面路由内,可以先获取到那个dom元素进行添加
  //例如:let el = document.querySelectorAll('#execute')[0]
 // el === undefined ? document.body.appendChild(instance.$el) : el.appendChild(instance.$el)注意判空
  Vue.nextTick(() => {
    // 设置弹窗为显示状态
    instance.openDialog()
  })
}
Vue.prototype.$executeResult = executeResult.install

4.再需要显示使用的地方:
使用
5.到此就完成了,结果展示
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

北北往前飞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值