index.js 937 Bytes
import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
Vue.use(VueRouter)
// 处理重复点击同一个路由报错的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

function getRoutes({ name, id, routesData }) {
  const routes = []
  if (routesData && name && id) {
    for (let i = 0, length = routesData.length; i < length; i += 1) {
      const element = routesData[i]
      if (element && element.path && element.path.includes(name) && element.meta && element.meta.id && element.meta.id.startsWith(id)) {
        const path = element.path.slice(name.length + 1)
        element.path = path
        element.component = () => import('@/views' + path + '.vue')
        routes.push(element)
      }
    }
  }
  return routes
}
export default {
  VueRouter,
  routes,
  getRoutes
}