index.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. import { createRouter, createWebHashHistory } from 'vue-router'
  2. import type { RouteRecordRaw } from 'vue-router'
  3. import { AppRouteRecordRaw } from './types'
  4. import type { App } from 'vue'
  5. import { getParentLayout } from './utils'
  6. /* Layout */
  7. const Layout = () => import('../layout/index.vue')
  8. /**
  9. * redirect: noredirect 当设置 noredirect 的时候该路由在面包屑导航中不可被点击
  10. * name:'router-name' 设定路由的名字,一定要填写不然使用<keep-alive>时会出现各种问题
  11. * meta : {
  12. hidden: true 当设置 true 的时候该路由不会再侧边栏出现 如404,login等页面(默认 false)
  13. alwaysShow: true 当你一个路由下面的 children 声明的路由大于1个时,自动会变成嵌套的模式,
  14. 只有一个时,会将那个子路由当做根路由显示在侧边栏,
  15. 若你想不管路由下面的 children 声明的个数都显示你的根路由,
  16. 你可以设置 alwaysShow: true,这样它就会忽略之前定义的规则,
  17. 一直显示根路由(默认 false)
  18. title: 'title' 设置该路由在侧边栏和面包屑中展示的名字
  19. icon: 'svg-name' 设置该路由的图标
  20. noCache: true 如果设置为true,则不会被 <keep-alive> 缓存(默认 false)
  21. breadcrumb: false 如果设置为false,则不会在breadcrumb面包屑中显示(默认 true)
  22. affix: true 如果设置为true,则会一直固定在tag项中(默认 false)
  23. noTagsView: true 如果设置为true,则不会出现在tag中(默认 false)
  24. activeMenu: '/dashboard' 显示高亮的路由路径
  25. }
  26. **/
  27. export const constantRouterMap: AppRouteRecordRaw[] = [
  28. {
  29. path: '/redirect',
  30. component: Layout,
  31. children: [
  32. {
  33. path: '/redirect/:path*',
  34. component: () => import('_c/Redirect/index.vue'),
  35. meta: {}
  36. }
  37. ],
  38. meta: {
  39. hidden: true
  40. }
  41. },
  42. {
  43. path: '/404',
  44. component: () => import('_c/Error/404.vue'),
  45. name: 'NoFind',
  46. meta: {
  47. hidden: true,
  48. title: '404',
  49. noTagsView: true
  50. }
  51. },
  52. {
  53. path: '/login',
  54. component: () => import('_p/index/views/login/index.vue'),
  55. name: 'Login',
  56. meta: {
  57. hidden: true,
  58. title: '登录',
  59. noTagsView: true
  60. }
  61. },
  62. {
  63. path: '/',
  64. component: Layout,
  65. redirect: '/dashboard',
  66. name: 'Root',
  67. meta: {},
  68. children: [
  69. {
  70. path: 'dashboard',
  71. component: () => import('_p/index/views/dashboard/index.vue'),
  72. name: 'Dashboard',
  73. meta: {
  74. title: '首页',
  75. icon: 'dashboard',
  76. noCache: true,
  77. affix: true
  78. }
  79. }
  80. ]
  81. },
  82. {
  83. path: '/external-link',
  84. component: Layout,
  85. meta: {},
  86. children: [
  87. {
  88. path: 'http://192.168.169.57/ue/2019/doc/vue-standard/dist/',
  89. meta: { title: '文档', icon: 'documentation' }
  90. }
  91. ]
  92. }
  93. ]
  94. export const asyncRouterMap: AppRouteRecordRaw[] = [
  95. {
  96. path: '/components-demo',
  97. component: Layout,
  98. redirect: '/components-demo/echarts',
  99. name: 'ComponentsDemo',
  100. meta: {
  101. title: '功能组件',
  102. icon: 'component',
  103. alwaysShow: true
  104. },
  105. children: [
  106. {
  107. path: 'echarts',
  108. component: () => import('_p/index/views/components-demo/echarts/index.vue'),
  109. name: 'EchartsDemo',
  110. meta: {
  111. title: '图表'
  112. }
  113. },
  114. {
  115. path: 'preview',
  116. component: () => import('_p/index/views/components-demo/preview/index.vue'),
  117. name: 'PreviewDemo',
  118. meta: {
  119. title: '图片预览'
  120. }
  121. },
  122. {
  123. path: 'count-to',
  124. component: () => import('_p/index/views/components-demo/count-to/index.vue'),
  125. name: 'CountToDemo',
  126. meta: {
  127. title: '数字动画'
  128. }
  129. },
  130. {
  131. path: 'search',
  132. component: () => import('_p/index/views/components-demo/search/index.vue'),
  133. name: 'SearchDemo',
  134. meta: {
  135. title: '查询'
  136. }
  137. },
  138. {
  139. path: 'editor',
  140. component: () => import('_p/index/views/components-demo/editor/index.vue'),
  141. name: 'EditorDemo',
  142. meta: {
  143. title: '富文本编辑器'
  144. }
  145. },
  146. {
  147. path: 'markdown',
  148. component: () => import('_p/index/views/components-demo/markdown/index.vue'),
  149. name: 'MarkdownDemo',
  150. meta: {
  151. title: 'markdown编辑器'
  152. }
  153. }
  154. ]
  155. },
  156. {
  157. path: '/table-demo',
  158. component: Layout,
  159. redirect: '/table-demo/basic-usage',
  160. name: 'TableDemo',
  161. meta: {
  162. title: '表格',
  163. icon: 'table',
  164. alwaysShow: true
  165. },
  166. children: [
  167. // {
  168. // path: 'test',
  169. // component: () => import('_p/index/views/table-demo/test'),
  170. // name: 'test',
  171. // meta: {
  172. // title: 'test'
  173. // }
  174. // },
  175. {
  176. path: 'basic-table',
  177. component: () => import('_p/index/views/table-demo/basic-table/index.vue'),
  178. name: 'BasicTable',
  179. meta: {
  180. title: '基础表格'
  181. }
  182. },
  183. {
  184. path: 'stripe-table',
  185. component: () => import('_p/index/views/table-demo/stripe-table/index.vue'),
  186. name: 'StripeTable',
  187. meta: {
  188. title: '带斑马纹表格'
  189. }
  190. },
  191. {
  192. path: 'border-table',
  193. component: () => import('_p/index/views/table-demo/border-table/index.vue'),
  194. name: 'BorderTable',
  195. meta: {
  196. title: '带边框表格'
  197. }
  198. },
  199. {
  200. path: 'state-table',
  201. component: () => import('_p/index/views/table-demo/state-table/index.vue'),
  202. name: 'StateTable',
  203. meta: {
  204. title: '带状态表格'
  205. }
  206. },
  207. {
  208. path: 'fixed-header',
  209. component: () => import('_p/index/views/table-demo/fixed-header/index.vue'),
  210. name: 'FixedHeader',
  211. meta: {
  212. title: '固定表头'
  213. }
  214. },
  215. {
  216. path: 'fixed-column',
  217. component: () => import('_p/index/views/table-demo/fixed-column/index.vue'),
  218. name: 'FixedColumn',
  219. meta: {
  220. title: '固定列'
  221. }
  222. },
  223. {
  224. path: 'fixed-column-header',
  225. component: () => import('_p/index/views/table-demo/fixed-column-header/index.vue'),
  226. name: 'FixedColumnHeader',
  227. meta: {
  228. title: '固定列和表头'
  229. }
  230. },
  231. {
  232. path: 'fluid-height',
  233. component: () => import('_p/index/views/table-demo/fluid-height/index.vue'),
  234. name: 'FluidHeight',
  235. meta: {
  236. title: '流体高度'
  237. }
  238. },
  239. {
  240. path: 'multi-header',
  241. component: () => import('_p/index/views/table-demo/multi-header/index.vue'),
  242. name: 'MultiHeader',
  243. meta: {
  244. title: '多级表头'
  245. }
  246. },
  247. {
  248. path: 'single-choice',
  249. component: () => import('_p/index/views/table-demo/single-choice/index.vue'),
  250. name: 'SingleChoice',
  251. meta: {
  252. title: '单选'
  253. }
  254. },
  255. {
  256. path: 'multiple-choice',
  257. component: () => import('_p/index/views/table-demo/multiple-choice/index.vue'),
  258. name: 'MultipleChoice',
  259. meta: {
  260. title: '多选'
  261. }
  262. },
  263. {
  264. path: 'sort-table',
  265. component: () => import('_p/index/views/table-demo/sort-table/index.vue'),
  266. name: 'SortTable',
  267. meta: {
  268. title: '排序'
  269. }
  270. },
  271. {
  272. path: 'screen-table',
  273. component: () => import('_p/index/views/table-demo/screen-table/index.vue'),
  274. name: 'ScreenTable',
  275. meta: {
  276. title: '筛选'
  277. }
  278. },
  279. {
  280. path: 'expand-row',
  281. component: () => import('_p/index/views/table-demo/expand-row/index.vue'),
  282. name: 'ExpandRow',
  283. meta: {
  284. title: '展开行'
  285. }
  286. },
  287. {
  288. path: 'tree-and-load',
  289. component: () => import('_p/index/views/table-demo/tree-and-load/index.vue'),
  290. name: 'TreeAndLoad',
  291. meta: {
  292. title: '树形数据与懒加载'
  293. }
  294. },
  295. {
  296. path: 'custom-header',
  297. component: () => import('_p/index/views/table-demo/custom-header/index.vue'),
  298. name: 'CustomHeader',
  299. meta: {
  300. title: '自定义表头'
  301. }
  302. },
  303. {
  304. path: 'total-table',
  305. component: () => import('_p/index/views/table-demo/total-table/index.vue'),
  306. name: 'TotalTable',
  307. meta: {
  308. title: '表尾合计行'
  309. }
  310. },
  311. ]
  312. },
  313. {
  314. path: '/directives-demo',
  315. component: Layout,
  316. redirect: '/directives-demo/clipboard',
  317. name: 'DirectivesDemo',
  318. meta: {
  319. title: '自定义指令',
  320. icon: 'clipboard',
  321. alwaysShow: true
  322. },
  323. children: [
  324. {
  325. path: 'clipboard',
  326. component: () => import('_p/index/views/directives-demo/clipboard/index.vue'),
  327. name: 'ClipboardDemo',
  328. meta: {
  329. title: 'Clipboard'
  330. }
  331. }
  332. ]
  333. },
  334. {
  335. path: '/hooks-demo',
  336. component: Layout,
  337. redirect: '/hooks-demo/watermark',
  338. name: 'HooksDemo',
  339. meta: {
  340. title: 'Hooks',
  341. icon: 'international',
  342. alwaysShow: true
  343. },
  344. children: [
  345. {
  346. path: 'watermark',
  347. component: () => import('_p/index/views/hooks-demo/useWatermark/index.vue'),
  348. name: 'UseWatermarkDemo',
  349. meta: {
  350. title: 'UseWaterMark'
  351. }
  352. },
  353. {
  354. path: 'useScrollTo',
  355. component: () => import('_p/index/views/hooks-demo/useScrollTo/index.vue'),
  356. name: 'UseScrollToDemo',
  357. meta: {
  358. title: 'UseScrollTo'
  359. }
  360. }
  361. ]
  362. },
  363. {
  364. path: '/icon',
  365. component: Layout,
  366. name: 'IconsDemo',
  367. meta: {},
  368. children: [
  369. {
  370. path: 'index',
  371. component: () => import('_p/index/views/icons/index.vue'),
  372. name: 'Icons',
  373. meta: {
  374. title: '图标',
  375. icon: 'icon'
  376. }
  377. }
  378. ]
  379. },
  380. {
  381. path: '/level',
  382. component: Layout,
  383. redirect: '/level/menu1/menu1-1/menu1-1-1',
  384. name: 'Level',
  385. meta: {
  386. title: '多级菜单缓存',
  387. icon: 'nested'
  388. },
  389. children: [
  390. {
  391. path: 'menu1',
  392. name: 'Menu1Demo',
  393. component: getParentLayout('Menu1Demo'),
  394. redirect: '/level/menu1/menu1-1/menu1-1-1',
  395. meta: {
  396. title: 'Menu1'
  397. },
  398. children: [
  399. {
  400. path: 'menu1-1',
  401. name: 'Menu11Demo',
  402. component: getParentLayout('Menu11Demo'),
  403. redirect: '/level/menu1/menu1-1/menu1-1-1',
  404. meta: {
  405. title: 'Menu1-1',
  406. alwaysShow: true
  407. },
  408. children: [
  409. {
  410. path: 'menu1-1-1',
  411. name: 'Menu111Demo',
  412. component: () => import('_p/index/views/level/Menu111.vue'),
  413. meta: {
  414. title: 'Menu1-1-1'
  415. }
  416. }
  417. ]
  418. },
  419. {
  420. path: 'menu1-2',
  421. name: 'Menu12Demo',
  422. component: () => import('_p/index/views/level/Menu12.vue'),
  423. meta: {
  424. title: 'Menu1-2'
  425. }
  426. }
  427. ]
  428. },
  429. {
  430. path: 'menu2',
  431. name: 'Menu2Demo',
  432. component: () => import('_p/index/views/level/Menu2.vue'),
  433. meta: {
  434. title: 'Menu2'
  435. }
  436. }
  437. ]
  438. },
  439. // {
  440. // path: '/example-demo',
  441. // component: Layout,
  442. // name: 'ExampleDemo',
  443. // meta: {
  444. // alwaysShow: true,
  445. // icon: 'example',
  446. // title: '综合实例'
  447. // },
  448. // children: [
  449. // {
  450. // path: 'example',
  451. // component: () => import('_p/index/views/example-demo/example/index.vue'),
  452. // name: 'Example',
  453. // meta: {
  454. // title: '列表综合实例'
  455. // }
  456. // }
  457. // ]
  458. // }
  459. ]
  460. const router = createRouter({
  461. history: createWebHashHistory(),
  462. strict: true,
  463. routes: constantRouterMap as RouteRecordRaw[]
  464. })
  465. export function resetRouter(): void {
  466. const resetWhiteNameList = [
  467. 'RedirectRoot',
  468. 'Redirect',
  469. 'Login',
  470. 'Root',
  471. 'Dashboard',
  472. 'Page404'
  473. ]
  474. router.getRoutes().forEach((route) => {
  475. const { name } = route
  476. if (name && !resetWhiteNameList.includes(name as string)) {
  477. router.hasRoute(name) && router.removeRoute(name)
  478. }
  479. })
  480. }
  481. export function setupRouter(app: App<Element>) {
  482. app.use(router)
  483. }
  484. export default router