index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import Vue from "vue";
  2. import VueRouter from "vue-router";
  3. import Home from "../views/home/Index.vue";
  4. Vue.use(VueRouter);
  5. const createRouter = () =>
  6. new VueRouter({
  7. mode: "history",
  8. base: process.env.BASE_URL,
  9. scrollBehavior: () => ({ y: 0 }),
  10. routes: [
  11. {
  12. path: "/",
  13. redirect: "/login",
  14. },
  15. {
  16. path: "/home",
  17. redirect: "/home/cockpitManage",
  18. name: "home",
  19. component: Home,
  20. children: [
  21. // 驾驶舱
  22. {
  23. id: 1,
  24. path: "cockpitManage",
  25. name: "驾驶舱",
  26. iconName: "gps",
  27. component: () =>
  28. import(
  29. /*webpackChunkName:'home-cockpitManage'*/ "../views/admin/cockpitManage/Index.vue"
  30. ),
  31. },
  32. {
  33. // 电子地图
  34. id: 11,
  35. path: "/home/cockpitManage/electronic-map",
  36. name: "电子地图",
  37. component: () =>
  38. import(
  39. /*webpackChunkName:'home-cockpitManage'*/ "../views/admin/cockpitManage/electronicMap.vue"
  40. ),
  41. },
  42. ],
  43. },
  44. {
  45. path: "/login",
  46. name: "login",
  47. component: () => import("../views/login/Index.vue"),
  48. },
  49. // 404 Page Not Found
  50. // {
  51. // path: "*",
  52. // name: "NotFound",
  53. // component: () => import("../views/error/404.vue"),
  54. // },
  55. ],
  56. });
  57. const router = createRouter();
  58. const originalPush = VueRouter.prototype.push;
  59. VueRouter.prototype.push = function push(location) {
  60. return originalPush.call(this, location).catch((err) => err);
  61. };
  62. // 重置路由
  63. export function resetRouter() {
  64. const newRouter = createRouter();
  65. router.matcher = newRouter.matcher; // reset router
  66. }
  67. router.beforeEach((to, from, next) => {
  68. console.log(to, "to");
  69. if (to.matched.length === 0) {
  70. return next("/login");
  71. } else {
  72. next();
  73. }
  74. });
  75. export default router;