From 635a1b90bb6de224cc192e52ec6a33d679a09a28 Mon Sep 17 00:00:00 2001 From: xingyy <64720302+Concur-max@users.noreply.github.com> Date: Fri, 10 Jan 2025 15:00:58 +0800 Subject: [PATCH] 12 --- app/components/x-message/useMessage.js | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 app/components/x-message/useMessage.js diff --git a/app/components/x-message/useMessage.js b/app/components/x-message/useMessage.js new file mode 100644 index 0000000..10eed19 --- /dev/null +++ b/app/components/x-message/useMessage.js @@ -0,0 +1,44 @@ +import { createApp, nextTick } from 'vue' +import MessagePopup from './index.vue' + +const message = { + success(text, duration = 2000) { + if (process.client) { + this.show({ type: 'success', message: text, duration }) + } + }, + error(text, duration = 2000) { + if (process.client) { + this.show({ type: 'error', message: text, duration }) + } + }, + warning(text, duration = 2000) { + if (process.client) { + this.show({ type: 'warning', message: text, duration }) + } + }, + show({ type = 'success', message, duration = 2000 }) { + if (!process.client) return + + const container = document.createElement('div') + document.body.appendChild(container) + + const app = createApp(MessagePopup, { + onAfterLeave: () => { + app.unmount() + document.body.removeChild(container) + } + }) + + const instance = app.mount(container) + nextTick(() => { + instance.showMessage?.({ + type, + message, + duration + }) + }) + } +} + +export { message } \ No newline at end of file