vue 防止Message 弹窗多次弹出

此版本为element-plus

1.首先在utils文件夹里面封装一个 重置弹窗的 js 文件。 名字: resetMessage.js

 
import { ElMessage } from 'element-plus' //引入message弹出框
 
let messageDom = null
const resetMessage = (options) => {
  if (messageDom) messageDom.close() // 判断弹窗是否已存在,若存在则关闭
   messageDom = ElMessage(options)
}
const typeArr = ['success', 'error', 'warning', 'info']
typeArr.forEach(type => {
  resetMessage[type] = options => {
    if (typeof options === 'string') options = { message: options }
    options.type = type
    return resetMessage(options)
  }
})
export const message = resetMessage

2.在mian.js中引用,全局挂载

import { message } from '@/utils/resetMessage.js' 
 
const app = createApp(App)
app.config.globalProperties.$msg = message; // 挂载

or

局部引用,局部挂载

import { message } from '@/utils/resetMessage.js' 

3.全局挂载使用方法

在需要使用弹窗的组件进行使用,但是需要引入 getCurrentInstance, 通过proxy.$msg来调用

import { getCurrentInstance } from 'vue'


submit(){
    const { proxy } = getCurrentInstance();// ctx中有问题,建议使用proxy中获取
    proxy.$msg.success('提交成功')
}

or

局部使用方法

import { message } from '@/utils/resetMessage.js'

btnClick(){
    message({
        title: '点击成功',
        type: 'success'
    })
}

管用的小伙伴们点点小爱心

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Vue.js制作一个弹出对话框组件,具体步骤如下: 1. 创建一个Vue组件,命名为Dialog: ```html <template> <div class="dialog-mask" v-show="visible"> <div class="dialog"> <div class="dialog-header">{{ title }}</div> <div class="dialog-body">{{ message }}</div> <div class="dialog-footer"> <button class="btn btn-primary" @click="confirm">确认</button> <button class="btn btn-default" @click="cancel">取消</button> </div> </div> </div> </template> <script> export default { data() { return { visible: false, title: '', message: '', confirmCallback: null, cancelCallback: null } }, methods: { confirm() { this.visible = false if (this.confirmCallback) { this.confirmCallback() } }, cancel() { this.visible = false if (this.cancelCallback) { this.cancelCallback() } } } } </script> ``` 这个组件包括了对话框的标题、内容、确认和取消按钮。它有一个visible属性,用于控制对话框的显示和隐藏。confirmCallback和cancelCallback是回调函数,用于在确认或取消按钮被点击时执行相应的操作。 2. 在需要使用对话框的地方,引入Dialog组件,并使用Vue的动态组件功能来动态渲染: ```html <template> <div> <button @click="showDialog">弹出对话框</button> <component :is="dialogComponent"></component> </div> </template> <script> import Dialog from './Dialog.vue' export default { data() { return { dialogComponent: null } }, components: { Dialog }, methods: { showDialog() { this.dialogComponent = Dialog this.$nextTick(() => { this.$refs.dialog.visible = true this.$refs.dialog.title = '提示' this.$refs.dialog.message = '确定要执行这个操作吗?' this.$refs.dialog.confirmCallback = () => { console.log('执行确认操作') } this.$refs.dialog.cancelCallback = () => { console.log('执行取消操作') } }) } } } </script> ``` 这个组件包含了一个按钮,点击按钮时会弹出对话框。它使用Vue的动态组件功能来动态渲染Dialog组件。在showDialog方法中,设置Dialog组件的属性,然后将Dialog组件赋值给dialogComponent属性。这样,Dialog组件就会被动态渲染到页面上,并显示出来。 上面的代码只是一个简单的示例,你可以根据你的需求来扩展它,如添加更多的按钮、设置对话框的样式等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值