当前位置:   article > 正文

Vue3 or: Unknown variable dynamic import: ../views/的解决方案_dynamic-import-helper:7 uncaught (in promise) erro

dynamic-import-helper:7 uncaught (in promise) error: unknown variable dynami

目录

​编辑

错误信息

原来的代码

修改后的代码


 

 

错误信息

  1. vue-router.mjs:3451 Error: Unknown variable dynamic import: ../views/BlogGather/DetailsArticlePage/pc/DetailsArticlePage.vue
  2. at dynamic-import-helper:7:96
  3. at new Promise (<anonymous>)
  4. at default (dynamic-import-helper:6:12)
  5. at details.js?t=1681893616671:15:20
  6. triggerError @ vue-router.mjs:3451
  7. (匿名) @ vue-router.mjs:3173
  8. Promise.catch(异步)
  9. pushWithRedirect @ vue-router.mjs:3167
  10. push @ vue-router.mjs:3099
  11. fromDetail @ ArticleCard.vue:95
  12. callWithErrorHandling @ runtime-core.esm-bundler.js:173
  13. callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:182
  14. invoker @ runtime-dom.esm-bundler.js:345
  15. dynamic-import-helper:7 Uncaught (in promise) Error: Unknown variable dynamic import: ../views/BlogGather/DetailsArticlePage/pc/DetailsArticlePage.vue
  16. at dynamic-import-helper:7:96
  17. at new Promise (<anonymous>)
  18. at default (dynamic-import-helper:6:12)
  19. at details.js?t=1681893616671:15:20

原来的代码
 

  1. const path = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i) ? 'mobile/' : 'pc/'
  2. console.log(path)
  3. export const routes=[
  4. {
  5. name: "index",
  6. path: "/",
  7. component: ()=>import(`../views/BlogGather/IndexPage/${path}IndexPage.vue`),
  8. meta: {title: "博客"},
  9. },
  10. {
  11. name: "xq",
  12. path: "/DetailsArticlePage",
  13. component: import(`../views/BlogGather/DetailsArticlePage/${path}DetailsArticlePage.vue`),
  14. meta: {title: "详情页面"},
  15. },
  16. ];

这样的写法在Vue2中是可以正常运行的但是在Vue3中就不可以了的。

修改后的代码

  1. const path = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i) ? 'mobile/' : 'pc/'
  2. console.log(path)
  3. let modules = import.meta.glob('../views/BlogGather/**/**/*.vue')
  4. console.log(modules)
  5. export const routes=[
  6. {
  7. name: "index",
  8. path: "/",
  9. component: ()=>import(`../views/BlogGather/IndexPage/${path}IndexPage.vue`),
  10. meta: {title: "博客"},
  11. },
  12. {
  13. name: "xq",
  14. path: "/DetailsArticlePage",
  15. component: modules[(`../views/BlogGather/DetailsArticlePage/${path}DetailsArticlePage.vue`)],
  16. meta: {title: "详情页面"},
  17. },
  18. ];

我们注意到,我们是先将所有的vue文件读取出来放到一个数组之中的。

然后再去数组中取值,这样才能动态的加载组件实现动态路由的效果。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号