catchAdmin.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { createApp } from 'vue';
  2. import type { App as app } from 'vue';
  3. import App from '@/App.vue';
  4. import router, { bootstrapRouter } from '@/router';
  5. import ElementPlus from 'element-plus';
  6. import zh from 'element-plus/es/locale/lang/zh-cn';
  7. import { bootstrapStore } from '@/stores';
  8. import Cache from './cache';
  9. import { bootstrapI18n } from '@/i18n';
  10. import guard from '@/router/guard';
  11. import { bootstrapDirectives } from '@/directives';
  12. import svgIcon from '@/components/SvgIcon/index.vue';
  13. /**
  14. * catchadmin
  15. */
  16. export default class CatchAdmin {
  17. protected app: app;
  18. protected element: string;
  19. /**
  20. * construct
  21. *
  22. * @param ele
  23. */
  24. constructor(ele = '#app') {
  25. this.app = createApp(App);
  26. this.element = ele;
  27. }
  28. /**
  29. * admin boot
  30. */
  31. bootstrap(): void {
  32. this.useElementPlus()
  33. .usePinia()
  34. .useI18n()
  35. .useRouter()
  36. .installDirectives()
  37. .installSvgComponent()
  38. .mount();
  39. }
  40. /**
  41. * 挂载节点
  42. *
  43. * @returns
  44. */
  45. protected mount(): void {
  46. this.app.mount(this.element);
  47. }
  48. /**
  49. * 加载路由
  50. *
  51. * @returns
  52. */
  53. protected useRouter(): CatchAdmin {
  54. guard(router);
  55. bootstrapRouter(this.app);
  56. return this;
  57. }
  58. /**
  59. * ui
  60. *
  61. * @returns
  62. */
  63. protected useElementPlus(): CatchAdmin {
  64. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  65. // @ts-ignore
  66. this.app.use(ElementPlus, {
  67. // locale: Cache.get('language') === 'zh' && zh
  68. locale: zh
  69. });
  70. return this;
  71. }
  72. /**
  73. * use pinia
  74. */
  75. protected usePinia(): CatchAdmin {
  76. bootstrapStore(this.app);
  77. return this;
  78. }
  79. /**
  80. * use i18n
  81. */
  82. protected useI18n(): CatchAdmin {
  83. bootstrapI18n(this.app);
  84. return this;
  85. }
  86. /**
  87. * install directives
  88. *
  89. * @protected
  90. */
  91. protected installDirectives(): CatchAdmin {
  92. bootstrapDirectives(this.app);
  93. return this;
  94. }
  95. /**
  96. * install -g svg-component
  97. *
  98. * @protected
  99. */
  100. protected installSvgComponent(): CatchAdmin {
  101. this.app.component('svg-icon', svgIcon);
  102. return this;
  103. }
  104. }