123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import Vue from 'vue'
- import VueRouter from 'vue-router'
- import { Menu,User,Detail } from '../config/route.js'
- import store from '@/store'
- Vue.use(VueRouter)
- const originalPush = VueRouter.prototype.push
- VueRouter.prototype.push = function push(location){
- return originalPush.call(this,location).catch(err=>err)
- }
- let routes = [];
- Menu.forEach((item) => {
- routes.push({
- name: item.name,
- path: `${item.link}`,
- meta: {
- id: item.belong
- },
- component: () => import(`../views/${item.name}/index.vue`)
- })
- })
- Detail.forEach((item) => {
- routes.push({
- name: item.name,
- path: `${item.link}`,
- meta: {
- id: item.belong
- },
- component: () => import(`../views/${item.name}/index.vue`)
- })
- })
- User.forEach((item) => {
- routes.push({
- name: item.name,
- path: `${item.link}`,
- meta: {
- id: item.belong,
- needlogin: item.needlogin,
- },
- component: () => import(`../views/user/${item.name}.vue`)
- })
- })
- const router = new VueRouter({
- routes
- })
- router.beforeEach((to, from, next) => {
-
- let userInfo = store.state.common.userInfo || {}
- if (to.meta.needlogin) {
- if (userInfo.userName) {
- window.scrollTo(0, 0)
- next()
- } else {
- window.scrollTo(0, 0)
- next({
- path: '/'
- })
- }
- }
- window.scrollTo(0, 0)
- next()
- })
- export default router
|