index.js 954 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import Vue from 'vue';
  2. import VueRouter from 'vue-router';
  3. import Store from '@/store/index';
  4. import routes from './routes/pages';
  5. import { routerMode } from '@/config/env';
  6. Vue.use(VueRouter);
  7. const Router = new VueRouter({
  8. mode: routerMode || 'history',
  9. base: process.env.BASE_URL,
  10. routes,
  11. scrollBehavior(to, from, savedPosition) {
  12. if (savedPosition) {
  13. return savedPosition;
  14. } else {
  15. return { x: 0, y: 0 };
  16. }
  17. }
  18. });
  19. // Add process
  20. Router.beforeEach((to, from, next) => {
  21. if (Store.state.user.token) {
  22. Store.commit('ADD_PROCESS', {
  23. label: to.name,
  24. value: to.path
  25. });
  26. next();
  27. } else {
  28. if (!to.path.includes('/login')) {
  29. return next('/login');
  30. }
  31. }
  32. next();
  33. });
  34. // Remove Navigating to current location (XXX) is not allowed
  35. const routerPush = VueRouter.prototype.push;
  36. VueRouter.prototype.push = function push(location) {
  37. return routerPush.call(this, location).catch(error => error);
  38. };
  39. export default Router;