asyncRouter.js 1.02 KB
/*
 * @Description:
 * @Autor: renchao
 * @LastEditTime: 2023-03-23 15:26:37
 */

var Layout;
let ApiUrl = localStorage.getItem('ApiUrl')
if (ApiUrl.THEME == 'sb') {
  Layout = r => require.ensure([], () => r(require(`@/layout1`)))
} else {
  Layout = r => require.ensure([], () => r(require(`@/layout`)))
}

/**
 * @description: filterAsyncRouter
 * @param {*} routers
 * @author: renchao
 */
export default function filterAsyncRouter (routers) {
  routers.forEach(item => {
    if (!item.children) {
      delete item.children
    } else {
      item.children = filterAsyncRouter(item.children)
    }
    item.path = JSON.parse(item.metadata)?.path || '/'
    if (!item.parentId) {
      item.component = Layout
    } else {
      item.component = loadView(item.uri)
    }
    item.meta = {
      title: item.name,
      icon: item.icon
    }
  })
  return routers
}
/**
 * @description: loadView
 * @param {*} view
 * @author: renchao
 */
function loadView (view) {
  return r => require.ensure([], () => r(require(`@/views${view}.vue`)))
}