Blame view

src/utils/asyncRouter.js 800 Bytes
1 2 3 4 5 6
/*
 * @Description: 动态路由
 * @Autor: renchao
 * @LastEditTime: 2023-05-16 14:08:42
 */

任超 committed
7
import Layout from '@/layout'
8 9 10 11 12
/**
 * @description: 
 * @param {*} routers
 * @author: renchao
 */
任超 committed
13 14 15 16
export default function filterAsyncRouter (routers) {
  routers.forEach(item => {
    if (!item.children) {
      delete item.children
任超 committed
17 18
    } else {
      item.children = filterAsyncRouter(item.children)
任超 committed
19
    }
任超 committed
20
    item.path = JSON.parse(item.metadata).path ? JSON.parse(item.metadata).path : '/'
任超 committed
21 22 23 24 25
    if (!item.parentId) {
      item.component = Layout
    } else {
      item.component = loadView(item.uri)
    }
任超 committed
26 27 28 29
    item.meta = {
      title: item.name,
      icon: item.icon
    }
任超 committed
30 31 32 33 34 35
  })
  return routers
}
function loadView (view) {
  return r => require.ensure([], () => r(require(`@/views${view}.vue`)))
}