index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // //创建一个路由器
  2. import { createRouter, createWebHashHistory,createWebHistory } from "vue-router";
  3. import configurator from "@/view/configurator.vue"
  4. import resultLeft from '@/view/result/resultLeft.vue';
  5. import resultRight from '@/view/result/resultRight.vue';
  6. const home = () => import("@/views/home/index.vue")
  7. import result from '@/view/result.vue';
  8. import { get } from "js-cookie";
  9. import { getToken2,setToken,setUserId } from "../utils/token";
  10. const router = createRouter({
  11. history: createWebHashHistory(),
  12. // history: createWebHistory(),
  13. routes: [
  14. {
  15. path: '/configurator',
  16. name:'模型库',
  17. component: configurator,// 这是路由的籁加载,也可以其他方式
  18. beforeEnter: (to, from,next) => {
  19. console.log(to, from);
  20. next()
  21. },
  22. },
  23. {
  24. path: '/vtk',
  25. name:'案例',
  26. component: () => import('@/view/vtk/vtk.vue'),// 这是路由的籁加载,也可以其他方式
  27. },
  28. {
  29. path: '/vtk2',
  30. name:'案例1',
  31. component: () => import('@/view/vtk/vtk2.vue'),// 这是路由的籁加载,也可以其他方式
  32. },
  33. {
  34. path: '/demo',
  35. name:'案例3',
  36. component: () => import('@/view/vtk/demo.vue'),// 这是路由的籁加载,也可以其他方式
  37. },
  38. // {
  39. // path: '/',
  40. // name:'首页',
  41. // component: () => import('@/view/home.vue'),
  42. // meta:{
  43. // keepAlive:false, // 需要缓存
  44. // title: '首页'
  45. // }
  46. // },
  47. {
  48. path: '/login',
  49. name: 'login',
  50. component: () => import('@/view/login.vue'), // 登录页面组件
  51. },
  52. {
  53. path: '/',
  54. name:'煤矿灾害态势推演',
  55. component: () => import('@/view/appmian.vue'),
  56. meta:{
  57. keepAlive:false, // 需要缓存
  58. title: '煤矿灾害态势推演'
  59. },
  60. children:[
  61. {
  62. path: '/home',
  63. name: 'home',
  64. component: () => import('@/views/home/index.vue'),
  65. }
  66. ]
  67. },
  68. // {
  69. // path: "/home",
  70. // name: "home",
  71. // component: home
  72. // },
  73. // {
  74. // path: '/home',
  75. // name:'局部',
  76. // component: () => import( "@/views/home/index.vue"),
  77. // meta:{
  78. // keepAlive:false, // 需要缓存
  79. // title: '局部'
  80. // },
  81. // },
  82. {
  83. path: '/goods',
  84. name: 'goods',
  85. component: {
  86. // 这里的 goods 对应 router-view 中的 name
  87. goods: resultLeft
  88. }
  89. },
  90. {
  91. path: '/orderinfo',
  92. name: 'orderinfo',
  93. component: {
  94. // 这里的 orderinfo 对应 router-view 中的 name
  95. orderinfo:resultRight
  96. }
  97. }
  98. // {
  99. // path: '/resultLeft',
  100. // name:'resultLeft',
  101. // component: resultLeft,// 这是路由的籁加载,也可以其他方式
  102. // },
  103. // {
  104. // path: '/resultRight',
  105. // name:'resultRight',
  106. // components:{
  107. // resultRight:resultRight
  108. // } // 这是路由的籁加载,也可以其他方式
  109. // },
  110. ]
  111. })
  112. //修改动态网页标题 beforeEach 导航钩子,路由改变前触发
  113. router.beforeEach((to,from,next) =>{
  114. //window.document.title = to.meta.title;
  115. // const urlParams = new URLSearchParams(to.fullPath);
  116. // const aids = urlParams.get('aid');
  117. let token = getToken2();
  118. console.log('守卫中Token:', token);
  119. if(to.fullPath.indexOf("aid")>0){
  120. setToken('e47b87eec69545559d1e81e56626da68');
  121. setUserId('5f06c8bc77234f969d13e160b54c27e3');
  122. console.log('守卫中aids:', to.fullPath);
  123. window.document.title = to.name;
  124. next();
  125. }else if (!token && to.name !== 'login') {
  126. // 如果没有 token 并且目标不是登录页,则重定向到登录页
  127. next({ name: 'login' });
  128. } else {
  129. window.document.title = to.name;
  130. next();
  131. }
  132. })
  133. router.afterEach((to,from,next) =>{
  134. window.scrollTo(0,0);
  135. })
  136. export default router;