util.import.production.js 1005 B

123456789101112131415161718192021222324252627282930
  1. /**
  2. * 生产环境
  3. * ===========
  4. * 当打包构建应用时,Javascript 包会变得非常大,影响页面加载。
  5. * 如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了。
  6. *
  7. */
  8. // module.exports = file => () => import('@/views' + file)
  9. module.exports = (file, params = {}) => {
  10. // 如果路径包含 `?`,提取参数
  11. const [path, query] = file.split('?')
  12. const component = import('@/views' + file)
  13. // 如果有参数,附加到组件
  14. if (query || Object.keys(params).length) {
  15. const queryParams = new URLSearchParams(query || '')
  16. const allParams = { ...Object.fromEntries(queryParams), ...params }
  17. // 方法仅初始化调用,返回 props 无用,菜单URL参数通过 defaultUrl 获取
  18. // return {
  19. // ...component,
  20. // props: allParams
  21. // }
  22. return component
  23. }
  24. return component
  25. }