123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import routerData from './router'
  4. import store from './../store/index'
  5. Vue.use(Router)
  6. const flatten = (parent, data) => {
  7. console.log(parent)
  8. return data.reduce((acc, { path, name, component, title, children }) => {
  9. const pth = [parent, path].join('/').replace('//', '/')
  10. const item = { path: pth, name, component, title, children: [] }
  11. if (children && children.length) {
  12. return acc.concat(item, flatten(pth, children))
  13. }
  14. return acc.concat(item)
  15. }, [])
  16. }
  17. // route 数据, 展平二级路由
  18. const routes = routerData.reduce((acc, r) => {
  19. const { children, ...rest } = r
  20. if (children && children.length) {
  21. return acc.concat({ ...rest, children: flatten([], children) })
  22. }
  23. return acc.concat(r)
  24. }, [])
  25. // console.log(routes)
  26. const router = new Router({
  27. routes,
  28. linkActiveClass: 'active',
  29. })
  30. router.beforeEach((to, from, next) => {
  31. store.commit('updateBread', { routerData, name: to.name })
  32. next()
  33. })
  34. export default router