index.ts 924 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
  2. import { dynamicRoutes } from "./module/dynamicRoutes";
  3. import { globalRoutes } from "./module/globalRoutes";
  4. import { useStore } from 'vuex'
  5. const routes: Array<RouteRecordRaw> = [
  6. ...dynamicRoutes,
  7. ...globalRoutes
  8. ]
  9. const router = createRouter({
  10. history: createWebHistory(process.env.BASE_URL),
  11. routes
  12. })
  13. /**
  14. * 设置标题
  15. */
  16. router.afterEach((to, from) => {
  17. document.title = '宝智达科技 - ' + to.meta.title;
  18. })
  19. router.beforeEach((to, from, next) => {
  20. const token = sessionStorage.getItem('User_tokey')
  21. console.log('拦截',token)
  22. if (token) {//直接放行
  23. next()
  24. } else {
  25. //如果是登录页面路径,就直接next()
  26. if (to.path === '/') {
  27. next();
  28. } else {
  29. next('/');
  30. }
  31. }
  32. if(to.name=='login'){//直接进入了登录,清除token
  33. sessionStorage.setItem('User_tokey','')
  34. }
  35. })
  36. export default router