index.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. redirect: '/components/icon',
  125. name: 'ComponentsDemo',
  126. meta: {
  127. title: t('router.component'),
  128. icon: 'bx:bxs-component',
  129. alwaysShow: true
  130. },
  131. children: [
  132. {
  133. path: 'form',
  134. component: getParentLayout(),
  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. name: 'TableDemo',
  171. meta: {
  172. title: t('router.table'),
  173. alwaysShow: true
  174. },
  175. children: [
  176. {
  177. path: 'default-table',
  178. component: () => import('@/views/Components/Table/DefaultTable.vue'),
  179. name: 'DefaultTable',
  180. meta: {
  181. title: t('router.defaultTable')
  182. }
  183. },
  184. {
  185. path: 'use-table',
  186. component: () => import('@/views/Components/Table/UseTableDemo.vue'),
  187. name: 'UseTable',
  188. meta: {
  189. title: 'UseTable'
  190. }
  191. },
  192. {
  193. path: 'ref-table',
  194. component: () => import('@/views/Components/Table/RefTable.vue'),
  195. name: 'RefTable',
  196. meta: {
  197. title: 'RefTable'
  198. }
  199. }
  200. ]
  201. },
  202. {
  203. path: 'editor-demo',
  204. component: getParentLayout(),
  205. name: 'EditorDemo',
  206. meta: {
  207. title: t('router.editor'),
  208. alwaysShow: true
  209. },
  210. children: [
  211. {
  212. path: 'editor',
  213. component: () => import('@/views/Components/Editor/Editor.vue'),
  214. name: 'Editor',
  215. meta: {
  216. title: t('router.richText')
  217. }
  218. }
  219. ]
  220. },
  221. {
  222. path: 'search',
  223. component: () => import('@/views/Components/Search.vue'),
  224. name: 'Search',
  225. meta: {
  226. title: t('router.search')
  227. }
  228. },
  229. {
  230. path: 'descriptions',
  231. component: () => import('@/views/Components/Descriptions.vue'),
  232. name: 'Descriptions',
  233. meta: {
  234. title: t('router.descriptions')
  235. }
  236. },
  237. {
  238. path: 'image-viewer',
  239. component: () => import('@/views/Components/ImageViewer.vue'),
  240. name: 'ImageViewer',
  241. meta: {
  242. title: t('router.imageViewer')
  243. }
  244. },
  245. {
  246. path: 'dialog',
  247. component: () => import('@/views/Components/Dialog.vue'),
  248. name: 'Dialog',
  249. meta: {
  250. title: t('router.dialog')
  251. }
  252. },
  253. {
  254. path: 'icon',
  255. component: () => import('@/views/Components/Icon.vue'),
  256. name: 'Icon',
  257. meta: {
  258. title: t('router.icon')
  259. }
  260. },
  261. {
  262. path: 'echart',
  263. component: () => import('@/views/Components/Echart.vue'),
  264. name: 'Echart',
  265. meta: {
  266. title: t('router.echart')
  267. }
  268. },
  269. {
  270. path: 'count-to',
  271. component: () => import('@/views/Components/CountTo.vue'),
  272. name: 'CountTo',
  273. meta: {
  274. title: t('router.countTo')
  275. }
  276. },
  277. {
  278. path: 'qrcode',
  279. component: () => import('@/views/Components/Qrcode.vue'),
  280. name: 'Qrcode',
  281. meta: {
  282. title: t('router.qrcode')
  283. }
  284. },
  285. {
  286. path: 'highlight',
  287. component: () => import('@/views/Components/Highlight.vue'),
  288. name: 'Highlight',
  289. meta: {
  290. title: t('router.highlight')
  291. }
  292. },
  293. {
  294. path: 'infotip',
  295. component: () => import('@/views/Components/Infotip.vue'),
  296. name: 'Infotip',
  297. meta: {
  298. title: t('router.infotip')
  299. }
  300. },
  301. {
  302. path: 'input-password',
  303. component: () => import('@/views/Components/InputPassword.vue'),
  304. name: 'InputPassword',
  305. meta: {
  306. title: t('router.inputPassword')
  307. }
  308. },
  309. {
  310. path: 'sticky',
  311. component: () => import('@/views/Components/Sticky.vue'),
  312. name: 'Sticky',
  313. meta: {
  314. title: t('router.sticky')
  315. }
  316. }
  317. ]
  318. },
  319. {
  320. path: '/hooks',
  321. component: Layout,
  322. redirect: '/hooks/useWatermark',
  323. name: 'Hooks',
  324. meta: {
  325. title: t('router.component'),
  326. icon: 'ic:outline-webhook',
  327. alwaysShow: true
  328. },
  329. children: [
  330. {
  331. path: 'useWatermark',
  332. component: () => import('@/views/hooks/useWatermark.vue'),
  333. name: 'UseWatermark',
  334. meta: {
  335. title: 'useWatermark'
  336. }
  337. }
  338. ]
  339. },
  340. {
  341. path: '/level',
  342. component: Layout,
  343. redirect: '/level/menu1/menu1-1/menu1-1-1',
  344. name: 'Level',
  345. meta: {
  346. title: t('router.level'),
  347. icon: 'carbon:skill-level-advanced'
  348. },
  349. children: [
  350. {
  351. path: 'menu1',
  352. name: 'Menu1',
  353. component: getParentLayout(),
  354. redirect: '/level/menu1/menu1-1/menu1-1-1',
  355. meta: {
  356. title: t('router.menu1')
  357. },
  358. children: [
  359. {
  360. path: 'menu1-1',
  361. name: 'Menu11',
  362. component: getParentLayout(),
  363. redirect: '/level/menu1/menu1-1/menu1-1-1',
  364. meta: {
  365. title: t('router.menu11'),
  366. alwaysShow: true
  367. },
  368. children: [
  369. {
  370. path: 'menu1-1-1',
  371. name: 'Menu111',
  372. component: () => import('@/views/Level/Menu111.vue'),
  373. meta: {
  374. title: t('router.menu111')
  375. }
  376. }
  377. ]
  378. },
  379. {
  380. path: 'menu1-2',
  381. name: 'Menu12',
  382. component: () => import('@/views/Level/Menu12.vue'),
  383. meta: {
  384. title: t('router.menu12')
  385. }
  386. }
  387. ]
  388. },
  389. {
  390. path: 'menu2',
  391. name: 'Menu2',
  392. component: () => import('@/views/Level/Menu2.vue'),
  393. meta: {
  394. title: t('router.menu2')
  395. }
  396. }
  397. ]
  398. },
  399. {
  400. path: '/example',
  401. component: Layout,
  402. redirect: '/example/example-dialog',
  403. name: 'Example',
  404. meta: {
  405. title: t('router.example'),
  406. icon: 'ep:management',
  407. alwaysShow: true
  408. },
  409. children: [
  410. {
  411. path: 'example-dialog',
  412. component: () => import('@/views/Example/Dialog/ExampleDialog.vue'),
  413. name: 'ExampleDialog',
  414. meta: {
  415. title: t('router.exampleDialog')
  416. }
  417. },
  418. {
  419. path: 'example-page',
  420. component: () => import('@/views/Example/Page/ExamplePage.vue'),
  421. name: 'ExamplePage',
  422. meta: {
  423. title: t('router.examplePage')
  424. }
  425. },
  426. {
  427. path: 'example-add',
  428. component: () => import('@/views/Example/Page/ExampleAdd.vue'),
  429. name: 'ExampleAdd',
  430. meta: {
  431. title: t('router.exampleAdd'),
  432. noTagsView: true,
  433. noCache: true,
  434. hidden: true,
  435. canTo: true,
  436. activeMenu: '/example/example-page'
  437. }
  438. },
  439. {
  440. path: 'example-edit',
  441. component: () => import('@/views/Example/Page/ExampleEdit.vue'),
  442. name: 'ExampleEdit',
  443. meta: {
  444. title: t('router.exampleEdit'),
  445. noTagsView: true,
  446. noCache: true,
  447. hidden: true,
  448. canTo: true,
  449. activeMenu: '/example/example-page'
  450. }
  451. },
  452. {
  453. path: 'example-detail',
  454. component: () => import('@/views/Example/Page/ExampleDetail.vue'),
  455. name: 'ExampleDetail',
  456. meta: {
  457. title: t('router.exampleDetail'),
  458. noTagsView: true,
  459. noCache: true,
  460. hidden: true,
  461. canTo: true,
  462. activeMenu: '/example/example-page'
  463. }
  464. }
  465. ]
  466. },
  467. {
  468. path: '/error',
  469. component: Layout,
  470. redirect: '/error/404',
  471. name: 'Error',
  472. meta: {
  473. title: t('router.errorPage'),
  474. icon: 'ci:error',
  475. alwaysShow: true
  476. },
  477. children: [
  478. {
  479. path: '404-demo',
  480. component: () => import('@/views/Error/404.vue'),
  481. name: '404Demo',
  482. meta: {
  483. title: '404'
  484. }
  485. },
  486. {
  487. path: '403-demo',
  488. component: () => import('@/views/Error/403.vue'),
  489. name: '403Demo',
  490. meta: {
  491. title: '403'
  492. }
  493. },
  494. {
  495. path: '500-demo',
  496. component: () => import('@/views/Error/500.vue'),
  497. name: '500Demo',
  498. meta: {
  499. title: '500'
  500. }
  501. }
  502. ]
  503. }
  504. // {
  505. // path: '/authorization',
  506. // component: Layout,
  507. // redirect: '/authorization/user',
  508. // name: 'Authorization',
  509. // meta: {
  510. // title: t('router.authorization'),
  511. // icon: 'eos-icons:role-binding',
  512. // alwaysShow: true
  513. // },
  514. // children: [
  515. // {
  516. // path: 'user',
  517. // component: () => import('@/views/Authorization/User.vue'),
  518. // name: 'User',
  519. // meta: {
  520. // title: t('router.user')
  521. // }
  522. // },
  523. // {
  524. // path: 'role',
  525. // component: () => import('@/views/Authorization/Role.vue'),
  526. // name: 'Role',
  527. // meta: {
  528. // title: t('router.role')
  529. // }
  530. // }
  531. // ]
  532. // }
  533. ]
  534. const router = createRouter({
  535. history: createWebHashHistory(),
  536. strict: true,
  537. routes: constantRouterMap as RouteRecordRaw[],
  538. scrollBehavior: () => ({ left: 0, top: 0 })
  539. })
  540. export const resetRouter = (): void => {
  541. const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  542. router.getRoutes().forEach((route) => {
  543. const { name } = route
  544. if (name && !resetWhiteNameList.includes(name as string)) {
  545. router.hasRoute(name) && router.removeRoute(name)
  546. }
  547. })
  548. }
  549. export const setupRouter = (app: App<Element>) => {
  550. app.use(router)
  551. }
  552. export default router