index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. // 加载条
  4. import NProgress from 'nprogress'
  5. import 'nprogress/nprogress.css'
  6. import VueAMap from 'vue-amap';
  7. Vue.use(VueRouter);
  8. // Vue.use(VueAMap);
  9. // VueAMap.initAMapApiLoader({
  10. // key: 'your amap key',
  11. // plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor'],
  12. // // 默认高德 sdk 版本为 1.4.4
  13. // v: '1.4.4'
  14. // });
  15. const routes = [
  16. {
  17. path: '/',
  18. component: () => import( '../views/Index.vue'),
  19. children: [
  20. {
  21. path: '/',
  22. name: 'Main',
  23. component: () => import('@/views/Main.vue'),
  24. meta: {
  25. title: "首页",
  26. clmid: "1",
  27. }
  28. },{
  29. path: '/rank',
  30. name: 'Rank',
  31. component: () => import('@/views/Rank.vue'),
  32. meta: {
  33. title: "排名",
  34. clmid: "2",
  35. }
  36. },{
  37. path: '/pk',
  38. name: 'pk',
  39. component: () => import('@/views/pk.vue'),
  40. meta: {
  41. title: "pk",
  42. clmid: "3",
  43. }
  44. },
  45. ]
  46. }, {
  47. path: '*',
  48. name: '404',
  49. component: () => import( '../views/404.vue')
  50. },
  51. ]
  52. const originalPush = VueRouter.prototype.push;
  53. VueRouter.prototype.push = function push(location) {
  54. return originalPush.call(this, location).catch(err => err)
  55. };
  56. const router = new VueRouter({
  57. // mode: 'history',
  58. mode: 'hash',
  59. base: process.env.BASE_URL,
  60. routes
  61. });
  62. // 路由守卫
  63. router.beforeEach((to,from,next)=>{
  64. NProgress.start();
  65. const isLogin = localStorage.token? true : false;
  66. if(to.path == '/login' || to.path == '/register'){//'login'和'register'相当于是路由白名单
  67. next();
  68. }else{
  69. //如果token存在,就正常跳转,如果不存在,则说明未登陆,则跳转到'login'
  70. // isLogin? next() : next("/login");
  71. next();
  72. }
  73. });
  74. router.afterEach(() => {
  75. NProgress.done()
  76. });
  77. export default router