index.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. import { createRouter, createWebHashHistory } from 'vue-router'
  2. import type { RouteRecordRaw } from 'vue-router'
  3. import type { App } from 'vue'
  4. import { Layout, getParentLayout } from '@/utils/routerHelper'
  5. import { useI18n } from '@/hooks/web/useI18n'
  6. const { t } = useI18n()
  7. export const constantRouterMap: AppRouteRecordRaw[] = [
  8. {
  9. path: '/',
  10. component: Layout,
  11. redirect: '/dashboard/analysis',
  12. name: 'Root',
  13. meta: {
  14. hidden: true
  15. }
  16. },
  17. {
  18. path: '/redirect',
  19. component: Layout,
  20. name: 'Redirect',
  21. children: [
  22. {
  23. path: '/redirect/:path(.*)',
  24. name: 'Redirect',
  25. component: () => import('@/views/Redirect/Redirect.vue'),
  26. meta: {}
  27. }
  28. ],
  29. meta: {
  30. hidden: true,
  31. noTagsView: true
  32. }
  33. },
  34. {
  35. path: '/login',
  36. component: () => import('@/views/Login/Login.vue'),
  37. name: 'Login',
  38. meta: {
  39. hidden: true,
  40. title: t('router.login'),
  41. noTagsView: true
  42. }
  43. },
  44. {
  45. path: '/404',
  46. component: () => import('@/views/Error/404.vue'),
  47. name: 'NoFind',
  48. meta: {
  49. hidden: true,
  50. title: '404',
  51. noTagsView: true
  52. }
  53. }
  54. ]
  55. export const asyncRouterMap: AppRouteRecordRaw[] = [
  56. {
  57. path: '/dashboard',
  58. component: Layout,
  59. redirect: '/dashboard/analysis',
  60. name: 'Dashboard',
  61. meta: {
  62. title: t('router.dashboard'),
  63. icon: 'ant-design:dashboard-filled',
  64. alwaysShow: true
  65. },
  66. children: [
  67. {
  68. path: 'analysis',
  69. component: () => import('@/views/Dashboard/Analysis.vue'),
  70. name: 'Analysis',
  71. meta: {
  72. title: t('router.analysis'),
  73. noCache: true,
  74. affix: true
  75. }
  76. },
  77. {
  78. path: 'workplace',
  79. component: () => import('@/views/Dashboard/Workplace.vue'),
  80. name: 'Workplace',
  81. meta: {
  82. title: t('router.workplace'),
  83. noCache: true
  84. }
  85. }
  86. ]
  87. },
  88. {
  89. path: '/external-link',
  90. component: Layout,
  91. meta: {},
  92. name: 'ExternalLink',
  93. children: [
  94. {
  95. path: 'https://element-plus-admin-doc.cn/',
  96. name: 'DocumentLink',
  97. meta: {
  98. title: t('router.document'),
  99. icon: 'clarity:document-solid'
  100. }
  101. }
  102. ]
  103. },
  104. {
  105. path: '/guide',
  106. component: Layout,
  107. name: 'Guide',
  108. meta: {},
  109. children: [
  110. {
  111. path: 'index',
  112. component: () => import('@/views/Guide/Guide.vue'),
  113. name: 'GuideDemo',
  114. meta: {
  115. title: t('router.guide'),
  116. icon: 'cib:telegram-plane'
  117. }
  118. }
  119. ]
  120. },
  121. {
  122. path: '/components',
  123. component: Layout,
  124. name: 'ComponentsDemo',
  125. meta: {
  126. title: t('router.component'),
  127. icon: 'bx:bxs-component',
  128. alwaysShow: true
  129. },
  130. children: [
  131. {
  132. path: 'form',
  133. component: getParentLayout(),
  134. redirect: '/components/form/default-form',
  135. name: 'Form',
  136. meta: {
  137. title: t('router.form'),
  138. alwaysShow: true
  139. },
  140. children: [
  141. {
  142. path: 'default-form',
  143. component: () => import('@/views/Components/Form/DefaultForm.vue'),
  144. name: 'DefaultForm',
  145. meta: {
  146. title: t('router.defaultForm')
  147. }
  148. },
  149. {
  150. path: 'use-form',
  151. component: () => import('@/views/Components/Form/UseFormDemo.vue'),
  152. name: 'UseForm',
  153. meta: {
  154. title: 'UseForm'
  155. }
  156. },
  157. {
  158. path: 'ref-form',
  159. component: () => import('@/views/Components/Form/RefForm.vue'),
  160. name: 'RefForm',
  161. meta: {
  162. title: 'RefForm'
  163. }
  164. }
  165. ]
  166. },
  167. {
  168. path: 'table',
  169. component: getParentLayout(),
  170. redirect: '/components/table/default-table',
  171. name: 'TableDemo',
  172. meta: {
  173. title: t('router.table'),
  174. alwaysShow: true
  175. },
  176. children: [
  177. {
  178. path: 'default-table',
  179. component: () => import('@/views/Components/Table/DefaultTable.vue'),
  180. name: 'DefaultTable',
  181. meta: {
  182. title: t('router.defaultTable')
  183. }
  184. },
  185. {
  186. path: 'use-table',
  187. component: () => import('@/views/Components/Table/UseTableDemo.vue'),
  188. name: 'UseTable',
  189. meta: {
  190. title: 'UseTable'
  191. }
  192. },
  193. {
  194. path: 'ref-table',
  195. component: () => import('@/views/Components/Table/RefTable.vue'),
  196. name: 'RefTable',
  197. meta: {
  198. title: 'RefTable'
  199. }
  200. }
  201. ]
  202. },
  203. {
  204. path: 'editor-demo',
  205. component: getParentLayout(),
  206. redirect: '/components/editor-demo/editor',
  207. name: 'EditorDemo',
  208. meta: {
  209. title: t('router.editor'),
  210. alwaysShow: true
  211. },
  212. children: [
  213. {
  214. path: 'editor',
  215. component: () => import('@/views/Components/Editor/Editor.vue'),
  216. name: 'Editor',
  217. meta: {
  218. title: t('router.richText')
  219. }
  220. }
  221. ]
  222. },
  223. {
  224. path: 'search',
  225. component: () => import('@/views/Components/Search.vue'),
  226. name: 'Search',
  227. meta: {
  228. title: t('router.search')
  229. }
  230. },
  231. {
  232. path: 'descriptions',
  233. component: () => import('@/views/Components/Descriptions.vue'),
  234. name: 'Descriptions',
  235. meta: {
  236. title: t('router.descriptions')
  237. }
  238. },
  239. {
  240. path: 'image-viewer',
  241. component: () => import('@/views/Components/ImageViewer.vue'),
  242. name: 'ImageViewer',
  243. meta: {
  244. title: t('router.imageViewer')
  245. }
  246. },
  247. {
  248. path: 'dialog',
  249. component: () => import('@/views/Components/Dialog.vue'),
  250. name: 'Dialog',
  251. meta: {
  252. title: t('router.dialog')
  253. }
  254. },
  255. {
  256. path: 'icon',
  257. component: () => import('@/views/Components/Icon.vue'),
  258. name: 'Icon',
  259. meta: {
  260. title: t('router.icon')
  261. }
  262. },
  263. {
  264. path: 'echart',
  265. component: () => import('@/views/Components/Echart.vue'),
  266. name: 'Echart',
  267. meta: {
  268. title: t('router.echart')
  269. }
  270. },
  271. {
  272. path: 'count-to',
  273. component: () => import('@/views/Components/CountTo.vue'),
  274. name: 'CountTo',
  275. meta: {
  276. title: t('router.countTo')
  277. }
  278. },
  279. {
  280. path: 'qrcode',
  281. component: () => import('@/views/Components/Qrcode.vue'),
  282. name: 'Qrcode',
  283. meta: {
  284. title: t('router.qrcode')
  285. }
  286. },
  287. {
  288. path: 'highlight',
  289. component: () => import('@/views/Components/Highlight.vue'),
  290. name: 'Highlight',
  291. meta: {
  292. title: t('router.highlight')
  293. }
  294. },
  295. {
  296. path: 'infotip',
  297. component: () => import('@/views/Components/Infotip.vue'),
  298. name: 'Infotip',
  299. meta: {
  300. title: t('router.infotip')
  301. }
  302. },
  303. {
  304. path: 'input-password',
  305. component: () => import('@/views/Components/InputPassword.vue'),
  306. name: 'InputPassword',
  307. meta: {
  308. title: t('router.inputPassword')
  309. }
  310. },
  311. {
  312. path: 'sticky',
  313. component: () => import('@/views/Components/Sticky.vue'),
  314. name: 'Sticky',
  315. meta: {
  316. title: t('router.sticky')
  317. }
  318. }
  319. ]
  320. },
  321. {
  322. path: '/hooks',
  323. component: Layout,
  324. redirect: '/hooks/useWatermark',
  325. name: 'Hooks',
  326. meta: {
  327. title: 'hooks',
  328. icon: 'ic:outline-webhook',
  329. alwaysShow: true
  330. },
  331. children: [
  332. {
  333. path: 'useWatermark',
  334. component: () => import('@/views/hooks/useWatermark.vue'),
  335. name: 'UseWatermark',
  336. meta: {
  337. title: 'useWatermark'
  338. }
  339. },
  340. {
  341. path: 'useCrudSchemas',
  342. component: () => import('@/views/hooks/useCrudSchemas.vue'),
  343. name: 'UseCrudSchemas',
  344. meta: {
  345. title: 'useCrudSchemas'
  346. }
  347. }
  348. ]
  349. },
  350. {
  351. path: '/level',
  352. component: Layout,
  353. redirect: '/level/menu1/menu1-1/menu1-1-1',
  354. name: 'Level',
  355. meta: {
  356. title: t('router.level'),
  357. icon: 'carbon:skill-level-advanced'
  358. },
  359. children: [
  360. {
  361. path: 'menu1',
  362. name: 'Menu1',
  363. component: getParentLayout(),
  364. redirect: '/level/menu1/menu1-1/menu1-1-1',
  365. meta: {
  366. title: t('router.menu1')
  367. },
  368. children: [
  369. {
  370. path: 'menu1-1',
  371. name: 'Menu11',
  372. component: getParentLayout(),
  373. redirect: '/level/menu1/menu1-1/menu1-1-1',
  374. meta: {
  375. title: t('router.menu11'),
  376. alwaysShow: true
  377. },
  378. children: [
  379. {
  380. path: 'menu1-1-1',
  381. name: 'Menu111',
  382. component: () => import('@/views/Level/Menu111.vue'),
  383. meta: {
  384. title: t('router.menu111')
  385. }
  386. }
  387. ]
  388. },
  389. {
  390. path: 'menu1-2',
  391. name: 'Menu12',
  392. component: () => import('@/views/Level/Menu12.vue'),
  393. meta: {
  394. title: t('router.menu12')
  395. }
  396. }
  397. ]
  398. },
  399. {
  400. path: 'menu2',
  401. name: 'Menu2',
  402. component: () => import('@/views/Level/Menu2.vue'),
  403. meta: {
  404. title: t('router.menu2')
  405. }
  406. }
  407. ]
  408. },
  409. {
  410. path: '/example',
  411. component: Layout,
  412. redirect: '/example/example-dialog',
  413. name: 'Example',
  414. meta: {
  415. title: t('router.example'),
  416. icon: 'ep:management',
  417. alwaysShow: true
  418. },
  419. children: [
  420. {
  421. path: 'example-dialog',
  422. component: () => import('@/views/Example/Dialog/ExampleDialog.vue'),
  423. name: 'ExampleDialog',
  424. meta: {
  425. title: t('router.exampleDialog')
  426. }
  427. },
  428. {
  429. path: 'example-page',
  430. component: () => import('@/views/Example/Page/ExamplePage.vue'),
  431. name: 'ExamplePage',
  432. meta: {
  433. title: t('router.examplePage')
  434. }
  435. },
  436. {
  437. path: 'example-add',
  438. component: () => import('@/views/Example/Page/ExampleAdd.vue'),
  439. name: 'ExampleAdd',
  440. meta: {
  441. title: t('router.exampleAdd'),
  442. noTagsView: true,
  443. noCache: true,
  444. hidden: true,
  445. canTo: true,
  446. activeMenu: '/example/example-page'
  447. }
  448. },
  449. {
  450. path: 'example-edit',
  451. component: () => import('@/views/Example/Page/ExampleEdit.vue'),
  452. name: 'ExampleEdit',
  453. meta: {
  454. title: t('router.exampleEdit'),
  455. noTagsView: true,
  456. noCache: true,
  457. hidden: true,
  458. canTo: true,
  459. activeMenu: '/example/example-page'
  460. }
  461. },
  462. {
  463. path: 'example-detail',
  464. component: () => import('@/views/Example/Page/ExampleDetail.vue'),
  465. name: 'ExampleDetail',
  466. meta: {
  467. title: t('router.exampleDetail'),
  468. noTagsView: true,
  469. noCache: true,
  470. hidden: true,
  471. canTo: true,
  472. activeMenu: '/example/example-page'
  473. }
  474. }
  475. ]
  476. },
  477. {
  478. path: '/error',
  479. component: Layout,
  480. redirect: '/error/404',
  481. name: 'Error',
  482. meta: {
  483. title: t('router.errorPage'),
  484. icon: 'ci:error',
  485. alwaysShow: true
  486. },
  487. children: [
  488. {
  489. path: '404-demo',
  490. component: () => import('@/views/Error/404.vue'),
  491. name: '404Demo',
  492. meta: {
  493. title: '404'
  494. }
  495. },
  496. {
  497. path: '403-demo',
  498. component: () => import('@/views/Error/403.vue'),
  499. name: '403Demo',
  500. meta: {
  501. title: '403'
  502. }
  503. },
  504. {
  505. path: '500-demo',
  506. component: () => import('@/views/Error/500.vue'),
  507. name: '500Demo',
  508. meta: {
  509. title: '500'
  510. }
  511. }
  512. ]
  513. }
  514. // {
  515. // path: '/authorization',
  516. // component: Layout,
  517. // redirect: '/authorization/user',
  518. // name: 'Authorization',
  519. // meta: {
  520. // title: t('router.authorization'),
  521. // icon: 'eos-icons:role-binding',
  522. // alwaysShow: true
  523. // },
  524. // children: [
  525. // {
  526. // path: 'user',
  527. // component: () => import('@/views/Authorization/User.vue'),
  528. // name: 'User',
  529. // meta: {
  530. // title: t('router.user')
  531. // }
  532. // },
  533. // {
  534. // path: 'role',
  535. // component: () => import('@/views/Authorization/Role.vue'),
  536. // name: 'Role',
  537. // meta: {
  538. // title: t('router.role')
  539. // }
  540. // }
  541. // ]
  542. // }
  543. ]
  544. const router = createRouter({
  545. history: createWebHashHistory(),
  546. strict: true,
  547. routes: constantRouterMap as RouteRecordRaw[],
  548. scrollBehavior: () => ({ left: 0, top: 0 })
  549. })
  550. export const resetRouter = (): void => {
  551. const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  552. router.getRoutes().forEach((route) => {
  553. const { name } = route
  554. if (name && !resetWhiteNameList.includes(name as string)) {
  555. router.hasRoute(name) && router.removeRoute(name)
  556. }
  557. })
  558. }
  559. export const setupRouter = (app: App<Element>) => {
  560. app.use(router)
  561. }
  562. export default router