index.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { createVNode, render } from 'vue';
  2. import Toast from '@/components/Toast/Toast.vue';
  3. import Confirm from '@/components/Toast/Confirm.vue'
  4. // 准备一个DOM容器
  5. const div = document.createElement('div');
  6. div.setAttribute('class', 'toast-wrapper');
  7. document.body.appendChild(div);
  8. const divConfirm = document.createElement('div');
  9. divConfirm.setAttribute('class', 'toast-wrapper-confirm');
  10. document.body.appendChild(divConfirm);
  11. let time: any = null;
  12. // 初始数据
  13. type title = string;
  14. interface ToastType {
  15. title: title;
  16. duration?: number;
  17. }
  18. interface showConfirmType {
  19. title?:string;
  20. text?:string;
  21. type?:string;
  22. callback?:Function;
  23. }
  24. type union = title | ToastType;
  25. let timer: any = null;
  26. export function show(options: union) {
  27. let title, duration;
  28. if (typeof options === 'string') {
  29. title = options || '我是默认文案';
  30. duration = 2000;
  31. } else {
  32. title = (options as ToastType).title || '我是默认文案';
  33. duration = (options as ToastType).duration || 2000;
  34. }
  35. // 创建虚拟dom (组件对象, props)
  36. const vnode = createVNode(Toast, { title });
  37. // 把虚拟dom渲染到div
  38. render(vnode, div);
  39. clearTimeout(timer);
  40. timer = setTimeout(() => {
  41. render(null, div);
  42. }, duration);
  43. }
  44. export function showConfirm(options:showConfirmType) {
  45. // 创建虚拟dom (组件对象, props)
  46. function close() {
  47. render(null, divConfirm);
  48. }
  49. const vnode = createVNode(Confirm, {options, close,});
  50. // 把虚拟dom渲染到div
  51. render(vnode, divConfirm);
  52. // clearTimeout(time);
  53. // time = setTimeout(() => {
  54. // render(null, divConfirm);
  55. // }, duration);
  56. }