index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import { Menu,User,Detail } from '../config/route.js'
  4. import store from '@/store'
  5. Vue.use(VueRouter)
  6. const originalPush = VueRouter.prototype.push
  7. VueRouter.prototype.push = function push(location){
  8. return originalPush.call(this,location).catch(err=>err)
  9. }
  10. let routes = [];
  11. Menu.forEach((item) => {
  12. routes.push({
  13. name: item.name,
  14. path: `${item.link}`,
  15. meta: {
  16. id: item.belong
  17. },
  18. component: () => import(`../views/${item.name}/index.vue`)
  19. })
  20. })
  21. Detail.forEach((item) => {
  22. routes.push({
  23. name: item.name,
  24. path: `${item.link}`,
  25. meta: {
  26. id: item.belong
  27. },
  28. component: () => import(`../views/${item.name}/index.vue`)
  29. })
  30. })
  31. User.forEach((item) => {
  32. routes.push({
  33. name: item.name,
  34. path: `${item.link}`,
  35. meta: {
  36. id: item.belong,
  37. needlogin: item.needlogin,
  38. },
  39. component: () => import(`../views/user/${item.name}.vue`)
  40. })
  41. })
  42. const router = new VueRouter({
  43. routes
  44. })
  45. router.beforeEach((to, from, next) => {
  46. let userInfo = store.state.common.userInfo || {}
  47. if (to.meta.needlogin) {
  48. if (userInfo.userName) {
  49. window.scrollTo(0, 0)
  50. next()
  51. } else {
  52. window.scrollTo(0, 0)
  53. next({
  54. path: '/'
  55. })
  56. }
  57. }
  58. window.scrollTo(0, 0)
  59. next()
  60. })
  61. export default router