vue異步組件與組件懶加載問(wèn)題(import不能導(dǎo)入變量字符串路徑)
vue異步組件與組件懶加載
在寫項(xiàng)目的時(shí)候,需要?jiǎng)討B(tài)配置路由菜單,所有的菜單都是通過(guò)配置生成的,這就意味著菜單的路徑(在vue開(kāi)發(fā)項(xiàng)目里面就是一個(gè)字符串的路徑)需要異步加載進(jìn)去,但是由于對(duì)webpack的import不是很熟悉,所以就有一下的坑需要填了
錯(cuò)誤的原因分析
_import.js module.exports = file => () => import(file)

但是這種方法錯(cuò)誤的原因是:
webpack 編譯es6 動(dòng)態(tài)引入 import() 時(shí)不能傳入變量,例如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)完全動(dòng)態(tài)。
解決方案一
可以通過(guò)字符串模板來(lái)提供部分信息給webpack;例如import(./path/${myFile}), 這樣編譯時(shí)會(huì)編譯所有./path下的模塊,但運(yùn)行時(shí)確定myFile的值才會(huì)加載,從而實(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
}
]
})
通過(guò)上述兩種方法都能夠解決 動(dòng)態(tài)組件的懶加載效果
vue懶加載組件 路徑不對(duì)
最近在使用VueRouter的懶加載組件的時(shí)候.
const routes = [
? ? {
? ? ? ? path: '/',
? ? ? ? component: App
? ? },
? ? {
? ? ? ? path: '/category',
? ? ? ? component: resolve => {require(['./components/Category.vue'], resolve)}
? ? }
]但是在加載/category的時(shí)候,我發(fā)現(xiàn)會(huì)報(bào)錯(cuò)。
原來(lái)webpack會(huì)把這個(gè)懶加載單獨(dú)加載一個(gè)js,默認(rèn)按照
0.app.js 1.app.js ……的順序加載的
通過(guò)簡(jiǎn)單的debug發(fā)現(xiàn)是0.app.js的路徑不對(duì)
通過(guò)webpack的設(shè)置即可解決(我使用的laravel,配置根據(jù)情況自行修改)
Elixir.webpack.mergeConfig({
? ? module: {
? ? ? ? loaders: [
? ? ? ? ? ? {
? ? ? ? ? ? ? ? test: /\.css/,
? ? ? ? ? ? ? ? loader: "style!css"
? ? ? ? ? ? }
? ? ? ? ]
? ? },
? ? output: {
? ? ? ? publicPath: "js/"
? ? }
})配置下output下的publicPath即可。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Vite使用unplugin-auto-import實(shí)現(xiàn)vue3中的自動(dòng)導(dǎo)入
- vue3項(xiàng)目如何配置按需自動(dòng)導(dǎo)入API組件unplugin-auto-import
- vue中import導(dǎo)入三種方式詳解
- Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹
- 解決vue3?defineProps?引入定義的接口報(bào)錯(cuò)
- 一文詳細(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)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
實(shí)現(xiàn)一個(gè)Vue版Upload組件
這篇文章主要介紹了實(shí)現(xiàn)一個(gè)Vue版Upload組件,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
Vue開(kāi)發(fā)實(shí)例探究key的作用詳解
這篇文章主要為大家介紹了Vue開(kāi)發(fā)實(shí)例探究key的作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
vue-cli 使用axios的操作方法及整合axios的多種方法
這篇文章主要介紹了vue-cli 使用axios的操作方法及整合axios的多種方法,vue-cli整合axios的多種方法,小編一一給大家列出來(lái)了,大家根據(jù)自身需要選擇,需要的朋友可以參考下2018-09-09
Vue數(shù)據(jù)驅(qū)動(dòng)表單渲染,輕松搞定form表單
這篇文章主要介紹了Vue數(shù)據(jù)驅(qū)動(dòng)表單渲染,輕松搞定form表單,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07

