vue3 + vite 动态加载?-灵析社区

满脑子智慧溢出

初始: const lazyLoad = (url: string) => import(`./${url}.vue`); 可以使用,但 vite 有警告: 09:45:57 [vite] warning: invalid import "./${url}.vue". Variable imports cannot import their own directory, place imports in a separate directory or make the import filename more specific. For example: import(`./foo/${bar}.js`). Plugin: vite:dynamic-import-vars File: D:/NHANH/WJT/project/intelligence-chart/src/components/configItem/config/main.ts 09:45:57 [vite] warning: D:/NHANH/WJT/project/intelligence-chart/src/components/configItem/config/main.ts 1 | import { Eye } from "@vicons/ionicons5"; 2 | const lazyLoad = (url) => import(`./${url}.vue`); | ^^^^^^^^^^^^^^ 3 | const Config = { 4 | echarts: { 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. 移动文件并改为: const lazyLoad = (url: string) => import(`./config/${url}.vue`); vite 没有警告了,但浏览器控制台报错: Uncaught (in promise) Error: Unknown variable dynamic import: ./config/echarts/line/data.vue 或是 不移动 文件改为: const lazyLoad = (url: string) => import(`../config/${url}.vue`); 都不行,这是为什么? 如何处理?

阅读量:126

点赞量:0

问AI
抄现成的解决方案吧 const modules = import.meta.glob('../views/**/*.{vue,tsx}') * "vue-element-plus-admin/src/utils/routerHelper.ts at master" (https://link.segmentfault.com/?enc=GFX%2F4oG0IaUcnNgQThLDBw%3D%3D.y0zacyGcYP%2Ff4KWP0YAue4JH9KtpR7b2lzfwcz69bQ6G5fePFKhlm8rLcUcr1dQ0JvfKhPSs1TDH%2F5N6NQaVFnR%2BT8O9gEWkiuheQdmQw%2BP2D9JAkSsDJ%2B8Wd4v0EAGn7jfm8jJz2uCT90VGTcqYEA%3D%3D) * "RuoYi-Vue3/src/store/modules/permission.js at master" (https://link.segmentfault.com/?enc=Y%2FMriJitfEKR28ubvFbr3Q%3D%3D.kTDobFREo5mZSWYpYxwWZXP4IzNlxaZY5jOKciaz3%2FBp5SaBw2CRQYU6OcPS4R6EEKXc6SP4D5x7FQ6rOApKdimq2oMOIe8AAs9Kiylt3LiMBDSpseLqN8pmnlzfrfpf) * "jeecgboot-vue3/src/router/helper/routeHelper.ts at master" (https://link.segmentfault.com/?enc=rKlNga2HjyCEIVm%2FSiRULA%3D%3D.ILt7fVVxfybg5Gq%2BR5%2Bxzn3QFhrSKkT6Yvvt2t4r8wdpY%2B29EDNY8wR3UNo0Ev2XTsJWP54RXPuP3%2BvjmIeY15OV8UrefYPpLibvvigZJdZl7BVBgd0nHgMxjbHYJ%2B7G)