vue異步組件與組件懶加載問題(import不能導入變量字符串路徑)
vue異步組件與組件懶加載
在寫項目的時候,需要動態(tài)配置路由菜單,所有的菜單都是通過配置生成的,這就意味著菜單的路徑(在vue開發(fā)項目里面就是一個字符串的路徑)需要異步加載進去,但是由于對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’),這是因為webpack的現(xiàn)在的實現(xiàn)方式不能實現(xiàn)完全動態(tài)。
解決方案一
可以通過字符串模板來提供部分信息給webpack;例如import(./path/${myFile}), 這樣編譯時會編譯所有./path下的模塊,但運行時確定myFile的值才會加載,從而實現(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)會報錯。
原來webpack會把這個懶加載單獨加載一個js,默認按照
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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Vite使用unplugin-auto-import實現(xiàn)vue3中的自動導入
- vue3項目如何配置按需自動導入API組件unplugin-auto-import
- vue中import導入三種方式詳解
- Vue export import 導入導出的多種方式與區(qū)別介紹
- 解決vue3?defineProps?引入定義的接口報錯
- 一文詳細聊聊vue3的defineProps、defineEmits和defineExpose
- Vue3中defineEmits、defineProps?不用引入便直接用
- vue3的defineExpose宏函數(shù)是如何暴露方法給父組件使用
- defineProps宏函數(shù)不需要從vue中import導入的原因解析
相關(guān)文章
Vue3動態(tài)使用KeepAlive組件的實現(xiàn)步驟
在 Vue 3 項目中,我們有時需要根據(jù)路由的 meta 信息來動態(tài)決定是否使用 KeepAlive 組件,以控制組件的緩存行為,所以本文給大家介紹了Vue3動態(tài)使用KeepAlive組件的實現(xiàn)步驟,通過代碼示例講解的非常詳細,需要的朋友可以參考下2024-11-11
vue點擊input彈出帶搜索鍵盤并監(jiān)聽該元素的方法
今天小編就為大家分享一篇vue點擊input彈出帶搜索鍵盤并監(jiān)聽該元素的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue從后端獲取到文件的?url?地址及前端根據(jù)?url?地址下載文件的實現(xiàn)思路
這篇文章主要介紹了vue?中從后端獲取到文件的?url?地址及前端根據(jù)?url?地址下載文件,項目用的是?vben?admin?框架,用的是?vue3?+?TS,后端返回的是文件的?url?地址,對vue后端獲取?url?地址的相關(guān)知識感興趣的朋友一起看看吧2024-02-02

