vue3?Error:Unknown?variable?dynamic?import:?../views/的解決方案
報錯截圖
錯誤信息
vue-router.mjs:3451 Error: Unknown variable dynamic import: ../views/BlogGather/DetailsArticlePage/pc/DetailsArticlePage.vue
at dynamic-import-helper:7:96
at new Promise (<anonymous>)
at default (dynamic-import-helper:6:12)
at details.js?t=1681893616671:15:20
triggerError @ vue-router.mjs:3451
(匿名) @ vue-router.mjs:3173
Promise.catch(異步)
pushWithRedirect @ vue-router.mjs:3167
push @ vue-router.mjs:3099
fromDetail @ ArticleCard.vue:95
callWithErrorHandling @ runtime-core.esm-bundler.js:173
callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:182
invoker @ runtime-dom.esm-bundler.js:345
dynamic-import-helper:7 Uncaught (in promise) Error: Unknown variable dynamic import: ../views/BlogGather/DetailsArticlePage/pc/DetailsArticlePage.vue
at dynamic-import-helper:7:96
at new Promise (<anonymous>)
at default (dynamic-import-helper:6:12)
at details.js?t=1681893616671:15:20
原來的代碼
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/' console.log(path) export const routes=[ { name: "index", path: "/", component: ()=>import(`../views/BlogGather/IndexPage/${path}IndexPage.vue`), meta: {title: "博客"}, }, { name: "xq", path: "/DetailsArticlePage", component: import(`../views/BlogGather/DetailsArticlePage/${path}DetailsArticlePage.vue`), meta: {title: "詳情頁面"}, }, ];
這樣的寫法在Vue2中是可以正常運行的但是在Vue3中就不可以了的。
修改后的代碼
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/' console.log(path) let modules = import.meta.glob('../views/BlogGather/**/**/*.vue') console.log(modules) export const routes=[ { name: "index", path: "/", component: ()=>import(`../views/BlogGather/IndexPage/${path}IndexPage.vue`), meta: {title: "博客"}, }, { name: "xq", path: "/DetailsArticlePage", component: modules[(`../views/BlogGather/DetailsArticlePage/${path}DetailsArticlePage.vue`)], meta: {title: "詳情頁面"}, }, ];
我們注意到,我們是先將所有的vue文件讀取出來放到一個數(shù)組之中的。
然后再去數(shù)組中取值,這樣才能動態(tài)的加載組件實現(xiàn)動態(tài)路由的效果。
總結(jié)
到此這篇關(guān)于vue3 Error:Unknown variable dynamic import: ../views/的解決方案的文章就介紹到這了,更多相關(guān)Unknown variable dynamic import內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue基礎(chǔ)popover彈出框編寫及bug問題分析
這篇文章主要為大家介紹了Vue基礎(chǔ)popover彈出框編寫及bug問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09vue 輸入框輸入任意內(nèi)容返回數(shù)字的實現(xiàn)
本文主要介紹了vue 輸入框輸入任意內(nèi)容返回數(shù)字的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03使用Vue-cli3.0創(chuàng)建的項目 如何發(fā)布npm包
這篇文章主要介紹了使用Vue-cli3.0創(chuàng)建的項目,如何發(fā)布npm包,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10Vue中Table組件行內(nèi)右鍵菜單實現(xiàn)方法(基于 vue + AntDesign)
這篇文章主要介紹了Vue中Table組件行內(nèi)右鍵菜單實現(xiàn)方法,該項目是基于 vue + AntDesign的,具體實例代碼給大家介紹的非常詳細(xì) ,需要的朋友可以參考下2019-11-11淺析Vue2和Vue3中雙向綁定機(jī)制的優(yōu)化與差異
Vue.js?核心特性之一就是雙向綁定,本文將深入探討?Vue2?和?Vue3?在雙向綁定上的區(qū)別,并分析這些改進(jìn)對性能和開發(fā)體驗的影響,希望對大家有所幫助2024-11-11