当前位置: 首页 > 工具软件 > Arco Design > 使用案例 >

vue3:arco design message封装

马凡
2023-12-01

1.新建一个message.ts

import { Message } from '@arco-design/web-vue'

// 定义一个唯一标识
const showMessage = Symbol('showMessage')

// 封装message类
class MessageBox {
    success(text: string, duration: number, show = true): void {
        this[showMessage]('success', text, duration , show)
    }
    error(text: string, duration: number, show = true): void {
        this[showMessage]('error', text, duration , show)
    }
    warning(text: string, duration: number, show = true): void {
        this[showMessage]('warning', text, duration , show)
    }
    info(text: string, duration: number, show = true): void {
        this[showMessage]('info', text, duration , show)
    }
    [showMessage](type: string, text: string, duration: number, show: boolean) {
        if (show) {
            // 判断是否已存在Message
            // if (document.getElementsByClassName('arco-message').length === 0) {
                Message[type]({
                    content:text,
                    duration,
                    closable:true
                })
            // }
        } else {
            // Message[type](text)
            Message[type]({
                content:text,
                duration,
                closable:true
            })
        }
    }
}

export default new MessageBox()```

2.在main.ts中导入message.ts文件

import { createApp } from ‘vue’
import ArcoVue from ‘@arco-design/web-vue’
import ArcoVueIcon from ‘@arco-design/web-vue/es/icon’
import MessageBox from ‘@/hooks/message’
app.use(ArcoVue, {})
app.use(ArcoVueIcon)
// 挂载到vue原型上
app.config.globalProperties.$message = MessageBox
app.use(router)
app.mount(‘#app’)

3、在页面上使用

import { getCurrentInstance } from ‘vue’
const { proxy }:any = getCurrentInstance() // 开发和生产都可以使用
proxy.$message.warning(‘操作成功’,10000*1000)


 类似资料: