PanJiaChen ,优化路由

https://github.com/PanJiaChen/vue-admin-template.git

添加组件path==null时, 路由

\src\store\modules\permission.js

import { constantRoutes } from '@/router'

function formatRoutes(routes) {

  const fmRoutes = []
  routes.forEach(router => {
    var routerStr = JSON.stringify(router)
    if (router.component) {
      let component = router.component
      if (component == 'Layout') {
        router.component = resolve => {
          require(['@/layout/index.vue'], resolve)
        }
        if (router.children.length > 0) {
          var hadIndex = false
          var redirect = ''
          for (var i = 0; i < router.children.length; i++) {
            if (router.children && router.children[i].path) {
              if (router.children[i].component.indexOf('/index') != -1) {
                hadIndex = true
                router.redirect = router.children[i].path
                break
              }else if (!redirect){
                redirect = router.children[i].path
              }
            }  
          }
          if (!hadIndex && !!redirect) {
            router.redirect = redirect
          }
          if (!redirect && !router.redirect || !router.path) {
            console.log('route = ' + routerStr)
            console.log('error route :' + JSON.stringify(router))
            return
          }
        }

        // if(router.children && router.children[0]){
        //   router.redirect = router.children[0].path
        // }
      } else {
        delete router.redirect
        router.component = resolve => {
          require(['@/views/' + component + '.vue'], resolve)
        }
      }
    }
    let children = router.children
    if (children && children instanceof Array) {
      let children = router.children.filter(item => item.component)
      if (children.length > 0) {
        children = formatRoutes(children)
        router.children = children
      } else {
        delete router.children
      }
    }
    if (!router.path) {
      console.log('illegal router : ' + JSON.stringify(router))
      return
    }
    fmRoutes.push(router)
  })

  return fmRoutes
}

const state = {
  menuIndex: parseInt(localStorage.getItem('menuIndex')) || 0,
  menus: [],
  routes: [],
  addRoutes: [],
  appKey: '',
  homeUrl: ''
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  },
  SET_MENUS: (state, menus) => {
    state.menus = menus
  },
  SET_MENUINDEX: (state, index) => {
    state.menuIndex = index
  },
  SET_APPKEY: (state, key) => {
    state.appKey = key
  },
  SET_HOMEURL: (state, url) => {
    state.homeUrl = url
  },
  SET_WSCONTEXT: (state, data) => {
    state.WSContext = data
  }
}

const actions = {
  generateRoutes({ commit, state }, { menus, path }) {
    if (path) {
      localStorage.path = path
      const index = menus.findIndex(item => item.path == path)
      commit('SET_MENUINDEX', index)
    }
    let index
    if (state.menuIndex + 1 > menus.length) {
      commit('SET_MENUINDEX', index = 0)
    } else {
      let str = window.location.href
      // if(str.indexOf("workBench?menuIndex=3") != -1){
      //   index=3
      //   commit('SET_MENUINDEX', 3)
      // } else
      if (str.indexOf('workBench?menuPath=work') != -1) {
        index = menus.findIndex(item => item.path == 'work')
        commit('SET_MENUINDEX', index)
      } else {
        index = state.menuIndex
      }
    }
    commit('SET_MENUS', menus)
    commit('SET_APPKEY', menus[index].path)
    const homeUrl = menus[index].homeUrl
    commit('SET_HOMEURL', homeUrl ? homeUrl : '')
    console.log('menus = ' + JSON.stringify(menus[index].children))
    return new Promise(resolve => {
      let accessedRoutes = formatRoutes(menus[index].children)
      accessedRoutes.push({ path: '*', redirect: '/', hidden: true })
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值