222
schangxiang@126.com
2025-04-30 9bec4dcae002f36aa23231da11cb03a156b40110
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { ElMessage, messageTypes } from 'element-plus'
 
function toast(
  messageOrOptions: string | Partial<any>,
  type: (typeof messageTypes)[number],
  duration?: any['duration']
) {
  let elMessage: ReturnType<typeof ElMessage>
 
  // fix: setTimeout 修复同时调用多个时会堆叠
  setTimeout(() => {
    elMessage = ElMessage({
      message: String(messageOrOptions),
      type,
      duration,
      grouping: true,
      showClose: true,
      ...Object(typeof messageOrOptions === 'object' ? messageOrOptions : {}),
    })
  })
 
  return {
    close() {
      setTimeout(() => {
        elMessage?.close()
      })
    },
  }
}
 
/**
 * 返回一个会自动关闭上一个的 toast
 */
function createSingleToast() {
  let single: ReturnType<typeof toast> | undefined
 
  return function singleToast(...args: Parameters<typeof toast>) {
    single?.close()
    single = toast(...args)
  }
}
export const singleToast = createSingleToast()
 
function warn(message: string) {
  toast(message, 'warning')
}
 
export { toast as default, createSingleToast, toast, warn }
 
// @ts-ignore
window.toast = toast