asyncRouter.js 909 B

12345678910111213141516171819202122232425262728293031
  1. const viewModules = import.meta.glob('../view/**/*.vue')
  2. const pluginModules = import.meta.glob('../plugin/**/*.vue')
  3. export const asyncRouterHandle = (asyncRouter) => {
  4. asyncRouter.forEach(item => {
  5. if (item.component && typeof item.component === 'string') {
  6. if (item.component.split('/')[0] === 'view') {
  7. item.component = dynamicImport(viewModules, item.component)
  8. } else if (item.component.split('/')[0] === 'plugin') {
  9. item.component = dynamicImport(pluginModules, item.component)
  10. }
  11. }
  12. if (item.children) {
  13. asyncRouterHandle(item.children)
  14. }
  15. })
  16. }
  17. function dynamicImport(
  18. dynamicViewsModules,
  19. component
  20. ) {
  21. const keys = Object.keys(dynamicViewsModules)
  22. const matchKeys = keys.filter((key) => {
  23. const k = key.replace('../', '')
  24. return k === component
  25. })
  26. const matchKey = matchKeys[0]
  27. return dynamicViewsModules[matchKey]
  28. }