index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import * as vueRouter from 'vue-router'
  2. import baseData from "./modules/base";
  3. import {getToken} from '~src/api/util/auth'
  4. import {getRouterData} from './routers'
  5. import NProgress from "nprogress";
  6. import "~src/styles/app/nprogress.scss";
  7. import config from "~src/config";
  8. //设置路由数据
  9. const router = vueRouter.createRouter({
  10. history: vueRouter.createWebHashHistory(),
  11. routes: baseData
  12. });
  13. //路由拦截
  14. router.beforeResolve(async (to) => {
  15. NProgress.start();
  16. const token = getToken()
  17. if (to.path === '/login') {
  18. return true
  19. } else if (to.path === '/auth') {
  20. return true
  21. } else if (!token) {
  22. return '/login'
  23. } else {
  24. return true
  25. /*const res = await getRouterData(to.name)
  26. if (res) {
  27. return true
  28. } else {
  29. return '/login'
  30. }*/
  31. }
  32. })
  33. router.afterEach((to) => {
  34. let pageTitle = to.meta['title'], title = config.title;
  35. if (to.path !== "/") {
  36. window.document.title = pageTitle?`${pageTitle} - ${title}`:title;
  37. } else {
  38. window.document.title = title;
  39. }
  40. window.scrollTo(0, 0)
  41. NProgress.done();
  42. })
  43. export function setupRouter(app){
  44. app.use(router)
  45. return router
  46. }
  47. export default router;