index.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. },
  303. {
  304. path: '/hooks',
  305. component: Layout,
  306. redirect: '/hooks/useWatermark',
  307. name: 'Hooks',
  308. meta: {
  309. title: t('router.component'),
  310. icon: 'ic:outline-webhook',
  311. alwaysShow: true
  312. },
  313. children: [
  314. {
  315. path: 'useWatermark',
  316. component: () => import('@/views/hooks/useWatermark.vue'),
  317. name: 'UseWatermark',
  318. meta: {
  319. title: 'useWatermark'
  320. }
  321. }
  322. ]
  323. },
  324. {
  325. path: '/level',
  326. component: Layout,
  327. redirect: '/level/menu1/menu1-1/menu1-1-1',
  328. name: 'Level',
  329. meta: {
  330. title: t('router.level'),
  331. icon: 'carbon:skill-level-advanced'
  332. },
  333. children: [
  334. {
  335. path: 'menu1',
  336. name: 'Menu1',
  337. component: getParentLayout(),
  338. redirect: '/level/menu1/menu1-1/menu1-1-1',
  339. meta: {
  340. title: t('router.menu1')
  341. },
  342. children: [
  343. {
  344. path: 'menu1-1',
  345. name: 'Menu11',
  346. component: getParentLayout(),
  347. redirect: '/level/menu1/menu1-1/menu1-1-1',
  348. meta: {
  349. title: t('router.menu11'),
  350. alwaysShow: true
  351. },
  352. children: [
  353. {
  354. path: 'menu1-1-1',
  355. name: 'Menu111',
  356. component: () => import('@/views/Level/Menu111.vue'),
  357. meta: {
  358. title: t('router.menu111')
  359. }
  360. }
  361. ]
  362. },
  363. {
  364. path: 'menu1-2',
  365. name: 'Menu12',
  366. component: () => import('@/views/Level/Menu12.vue'),
  367. meta: {
  368. title: t('router.menu12')
  369. }
  370. }
  371. ]
  372. },
  373. {
  374. path: 'menu2',
  375. name: 'Menu2',
  376. component: () => import('@/views/Level/Menu2.vue'),
  377. meta: {
  378. title: t('router.menu2')
  379. }
  380. }
  381. ]
  382. },
  383. {
  384. path: '/example',
  385. component: Layout,
  386. redirect: '/example/example-dialog',
  387. name: 'Example',
  388. meta: {
  389. title: t('router.example'),
  390. icon: 'ep:management',
  391. alwaysShow: true
  392. },
  393. children: [
  394. {
  395. path: 'example-dialog',
  396. component: () => import('@/views/Example/Dialog/ExampleDialog.vue'),
  397. name: 'ExampleDialog',
  398. meta: {
  399. title: t('router.exampleDialog')
  400. }
  401. },
  402. {
  403. path: 'example-page',
  404. component: () => import('@/views/Example/Page/ExamplePage.vue'),
  405. name: 'ExamplePage',
  406. meta: {
  407. title: t('router.examplePage')
  408. }
  409. },
  410. {
  411. path: 'example-add',
  412. component: () => import('@/views/Example/Page/ExampleAdd.vue'),
  413. name: 'ExampleAdd',
  414. meta: {
  415. title: t('router.exampleAdd'),
  416. noTagsView: true,
  417. noCache: true,
  418. hidden: true,
  419. showMainRoute: true,
  420. activeMenu: '/example/example-page'
  421. }
  422. },
  423. {
  424. path: 'example-edit',
  425. component: () => import('@/views/Example/Page/ExampleEdit.vue'),
  426. name: 'ExampleEdit',
  427. meta: {
  428. title: t('router.exampleEdit'),
  429. noTagsView: true,
  430. noCache: true,
  431. hidden: true,
  432. showMainRoute: true,
  433. activeMenu: '/example/example-page'
  434. }
  435. },
  436. {
  437. path: 'example-detail',
  438. component: () => import('@/views/Example/Page/ExampleDetail.vue'),
  439. name: 'ExampleDetail',
  440. meta: {
  441. title: t('router.exampleDetail'),
  442. noTagsView: true,
  443. noCache: true,
  444. hidden: true,
  445. showMainRoute: true,
  446. activeMenu: '/example/example-page'
  447. }
  448. }
  449. ]
  450. },
  451. {
  452. path: '/error',
  453. component: Layout,
  454. redirect: '/error/404',
  455. name: 'Error',
  456. meta: {
  457. title: t('router.errorPage'),
  458. icon: 'ci:error',
  459. alwaysShow: true
  460. },
  461. children: [
  462. {
  463. path: '404-demo',
  464. component: () => import('@/views/Error/404.vue'),
  465. name: '404Demo',
  466. meta: {
  467. title: '404'
  468. }
  469. },
  470. {
  471. path: '403-demo',
  472. component: () => import('@/views/Error/403.vue'),
  473. name: '403Demo',
  474. meta: {
  475. title: '403'
  476. }
  477. },
  478. {
  479. path: '500-demo',
  480. component: () => import('@/views/Error/500.vue'),
  481. name: '500Demo',
  482. meta: {
  483. title: '500'
  484. }
  485. }
  486. ]
  487. }
  488. // {
  489. // path: '/authorization',
  490. // component: Layout,
  491. // redirect: '/authorization/user',
  492. // name: 'Authorization',
  493. // meta: {
  494. // title: t('router.authorization'),
  495. // icon: 'eos-icons:role-binding',
  496. // alwaysShow: true
  497. // },
  498. // children: [
  499. // {
  500. // path: 'user',
  501. // component: () => import('@/views/Authorization/User.vue'),
  502. // name: 'User',
  503. // meta: {
  504. // title: t('router.user')
  505. // }
  506. // },
  507. // {
  508. // path: 'role',
  509. // component: () => import('@/views/Authorization/Role.vue'),
  510. // name: 'Role',
  511. // meta: {
  512. // title: t('router.role')
  513. // }
  514. // }
  515. // ]
  516. // }
  517. ]
  518. const router = createRouter({
  519. history: createWebHashHistory(),
  520. strict: true,
  521. routes: constantRouterMap as RouteRecordRaw[],
  522. scrollBehavior: () => ({ left: 0, top: 0 })
  523. })
  524. export const resetRouter = (): void => {
  525. const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  526. router.getRoutes().forEach((route) => {
  527. const { name } = route
  528. if (name && !resetWhiteNameList.includes(name as string)) {
  529. router.hasRoute(name) && router.removeRoute(name)
  530. }
  531. })
  532. }
  533. export const setupRouter = (app: App<Element>) => {
  534. app.use(router)
  535. }
  536. export default router