关于vue异步加载组件,动态路由加载组件的问题

目前使用vite搭建后台管理系统,遇到了异步加载路由组件的问题,加上之前vue2的时候异步加载路由组件一起做个整理,之前做过vue2,所以vue2和vue2一起做个整理,先说vue3。

当前使用vue3创建动态组件路由方法

const dynamicRoutes = [
   {
        ...,
        component: () => import('@/views/xx.vue')
    }
]

但通常的话后端会返回组件的地址,import里就会传个动态值,值为组件路径,但使用vite&vue3搭建的话会有告警

component: () => import(obj.componentPath)
The above dynamic import cannot be analyzed by Vite.
See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats. If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning.

网上搜了vue3+vite有两种方案

  1. 使用vue3的API defineAsyncComponent加上注释 / @vite-ignore /
  2. 使用import.meta.glob 这种方法尝试后失败,就没深入研究
  3. shadowRef和defineAsyncComponent配合使用,还没尝试

你可能感兴趣的:(vue3vue-router4)