vue異步組件與組件懶加載問題(import不能導(dǎo)入變量字符串路徑)
vue異步組件與組件懶加載
在寫項(xiàng)目的時候,需要動態(tài)配置路由菜單,所有的菜單都是通過配置生成的,這就意味著菜單的路徑(在vue開發(fā)項(xiàng)目里面就是一個字符串的路徑)需要異步加載進(jìn)去,但是由于對webpack的import不是很熟悉,所以就有一下的坑需要填了
錯誤的原因分析
_import.js module.exports = file => () => import(file)
但是這種方法錯誤的原因是:
webpack 編譯es6 動態(tài)引入 import() 時不能傳入變量,例如dir =’path/to/my/file.js’ ; import(dir) , 而要傳入字符串 import(‘path/to/my/file.js’),這是因?yàn)閣ebpack的現(xiàn)在的實(shí)現(xiàn)方式不能實(shí)現(xiàn)完全動態(tài)。
解決方案一
可以通過字符串模板來提供部分信息給webpack;例如import(./path/${myFile}), 這樣編譯時會編譯所有./path下的模塊,但運(yùn)行時確定myFile的值才會加載,從而實(shí)現(xiàn)懶加載。
解決方案二
function lazyLoadView(AsyncView) { const AsyncHandler = () => ({ component: AsyncView, // A component to use while the component is loading. loading: require('@/view/system/Loading.vue').default, // A fallback component in case the timeout is exceeded // when loading the component. error: require('@/view/system/Timeout.vue').default, // Delay before showing the loading component. // Default: 200 (milliseconds). delay: 200, // Time before giving up trying to load the component. // Default: Infinity (milliseconds). timeout: 10000 }); return Promise.resolve({ functional: true, render(h, { data, children }) { // Transparently pass any props or children // to the view component. return h(AsyncHandler, data, children); } }); } const My = () => lazyLoadView(import('@/view/My.vue')); const router = new VueRouter({ routes: [ { path: '/my', component: My } ] })
通過上述兩種方法都能夠解決 動態(tài)組件的懶加載效果
vue懶加載組件 路徑不對
最近在使用VueRouter的懶加載組件的時候.
const routes = [ ? ? { ? ? ? ? path: '/', ? ? ? ? component: App ? ? }, ? ? { ? ? ? ? path: '/category', ? ? ? ? component: resolve => {require(['./components/Category.vue'], resolve)} ? ? } ]
但是在加載/category的時候,我發(fā)現(xiàn)會報(bào)錯。
原來webpack會把這個懶加載單獨(dú)加載一個js,默認(rèn)按照
0.app.js 1.app.js ……的順序加載的
通過簡單的debug發(fā)現(xiàn)是0.app.js的路徑不對
通過webpack的設(shè)置即可解決(我使用的laravel,配置根據(jù)情況自行修改)
Elixir.webpack.mergeConfig({ ? ? module: { ? ? ? ? loaders: [ ? ? ? ? ? ? { ? ? ? ? ? ? ? ? test: /\.css/, ? ? ? ? ? ? ? ? loader: "style!css" ? ? ? ? ? ? } ? ? ? ? ] ? ? }, ? ? output: { ? ? ? ? publicPath: "js/" ? ? } })
配置下output下的publicPath即可。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- Vite使用unplugin-auto-import實(shí)現(xiàn)vue3中的自動導(dǎo)入
- vue3項(xiàng)目如何配置按需自動導(dǎo)入API組件unplugin-auto-import
- vue中import導(dǎo)入三種方式詳解
- Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹
- 解決vue3?defineProps?引入定義的接口報(bào)錯
- 一文詳細(xì)聊聊vue3的defineProps、defineEmits和defineExpose
- Vue3中defineEmits、defineProps?不用引入便直接用
- vue3的defineExpose宏函數(shù)是如何暴露方法給父組件使用
- defineProps宏函數(shù)不需要從vue中import導(dǎo)入的原因解析
相關(guān)文章
Vue3中如何修改父組件傳遞到子組件中的值(全網(wǎng)少有!)
大家都知道,vue是具有單向數(shù)據(jù)流的傳遞特性,下面這篇文章主要給大家介紹了關(guān)于Vue3中如何修改父組件傳遞到子組件中值的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04vue-cli 使用axios的操作方法及整合axios的多種方法
這篇文章主要介紹了vue-cli 使用axios的操作方法及整合axios的多種方法,vue-cli整合axios的多種方法,小編一一給大家列出來了,大家根據(jù)自身需要選擇,需要的朋友可以參考下2018-09-09Vue數(shù)據(jù)驅(qū)動表單渲染,輕松搞定form表單
這篇文章主要介紹了Vue數(shù)據(jù)驅(qū)動表單渲染,輕松搞定form表單,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07