vue中路由自動導(dǎo)入的方式是什么?
Admin 2021-10-23 群英技術(shù)資訊 1388 次瀏覽
vue中路由自動導(dǎo)入的方式是什么?vue中導(dǎo)入路由是很基礎(chǔ)的操作,本文給大家分享的是vue中路由自動導(dǎo)入的方法,下文還有示例供大家參考,感興趣的朋友就繼續(xù)往下看吧。
我們使用的是require.context方法導(dǎo)入,在vite創(chuàng)建的項目內(nèi)使用會報錯"require not found",所以必須用webpack創(chuàng)建項目。或者有大能可以說說vite怎么解決這個問題。
我們使用的規(guī)則是,搜索src/views/路徑下的所有目錄和子目錄,搜索文件名叫做"index.vue"的文件,使用上級目錄的名字作為組件名,進行注冊。結(jié)構(gòu)如下:

和公共組件注冊一樣,我們只注冊index.vue組件,其他名稱的組件不進行注冊。
// src/router/index.ts
import {createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw} from 'vue-router'
import store from "@/store";
import daxie from "@/util/upper"; // 引入一個方法,將字符串的首字母進行大寫,我習(xí)慣將pathname首字母大寫
// 路由自動化注冊
const routerList:any = []
const requireComponent = require.context('@/views', true, /index.vue$/) // 找到views路徑下的所有文件
const dynamic_route = requireComponent.keys().filter(fileName => {
return true
})
// 現(xiàn)在文件數(shù)組是亂序的,我們首先進行排序,父級路由在前面,如果是子級路由在前面,結(jié)果父級理由還沒有創(chuàng)建,就會報錯
// console.log(dynamic_route,"排序前")
dynamic_route.sort(function (a,b):number{
const jieguoa:any = a.split("").filter((item:string)=>{
return "/" == item
})
const jieguob:any = b.split("").filter((item:string)=>{
return "/" == item
})
if(jieguoa.length<jieguob.length){return -1}
if(jieguoa.length>jieguob.length){return 1}
return 0
})
// console.log(dynamic_route,"排序后")
dynamic_route.forEach(fileName => {
const path = fileName.replace(".", "")
const namelist = fileName.replace(".", "").replace("index.vue", "").split("/").filter((i:any) => {
return i
})
// 測試配置
const componentConfig = requireComponent(fileName)
// 組件可以隨意添加任何屬性,目前添加一個canshu屬性,是一個數(shù)組,里面存放著需要的動態(tài)參數(shù)
// console.log(componentConfig.default,"組件配置2")
// 每一層都需要手動指定,只做三層吧
if(namelist.length == 1){
routerList.push({
path:"/"+ namelist[namelist.length - 1],
name: daxie(namelist[namelist.length-1]),
component:()=>import(`../views${path}`),
children:[],
})
}else if(namelist.length == 2){
routerList.forEach((item:any)=>{
if(item.name == daxie(namelist[0])){
// 判斷組件是否需要參數(shù)
const canshu = componentConfig.default.canshu || []
if(canshu){
for (let i=0;i<canshu.length;i++){
canshu[i] = "/:"+canshu[i]
}
item.children.push({
path: namelist[namelist.length-1] + canshu.join(""),
name: daxie(namelist[namelist.length-1]),
component:()=>import(`../views${path}`),
children:[],
})
}else{
item.children.push({
path: namelist[namelist.length-1],
name: daxie(namelist[namelist.length-1]),
component:()=>import(`../views${path}`),
children:[],
})
}
}
})
}else if(namelist.length == 3){
routerList.forEach((item:any)=>{
if(item.name == daxie(namelist[0])){
item.children.forEach((yuansu:any)=>{
if(yuansu.name == daxie(namelist[1])){
// 判斷是不是需要參數(shù)
const canshu = componentConfig.default.canshu || []
if(canshu){
for (let i=0;i<canshu.length;i++){
canshu[i] = "/:"+canshu[i]
}
yuansu.children.push({
path: namelist[namelist.length - 1]+canshu.join(""),
name: daxie(namelist[namelist.length-1]),
component:()=>import(`../views${path}`),
})
}else {
yuansu.children.push({
path: namelist[namelist.length - 1],
name: daxie(namelist[namelist.length-1]),
component:()=>import(`../views${path}`),
})
}
}
})
}
})
}
})
const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'Home',
component: ()=>import("@/views/shouye/shouye.vue")
},
{
path: '/about',
name: 'About',
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
},
...routerList,
{
path: '/404',
name: '404',
component: () => import('@/views/404.vue')
},
{
path: '/:pathMatch(.*)',
redirect: '/404'
},
]
// 注意順序,根據(jù)最新的路由匹配規(guī)則,404頁面必須在最后,
console.log(routes,"查看路由表內(nèi)容")
const router = createRouter({
history: createWebHistory(),
// history: createWebHashHistory(),
routes
})
export default router
這樣,只需要根據(jù)規(guī)則創(chuàng)建組件,就會被自動注冊到路由里面。免去手動注冊的繁瑣操作。
關(guān)于vue路由導(dǎo)入方式就分享到這,上述實例具有一定的借鑒價值,需要的朋友可以參考,希望能對大家有幫助,想要了解更多vue路由導(dǎo)入方式,大家可以關(guān)注其它的相關(guān)文章。
文本轉(zhuǎn)載自腳本之家
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:[email protected]進行舉報,并提供相關(guān)證據(jù),查實之后,將立刻刪除涉嫌侵權(quán)內(nèi)容。
猜你喜歡
目錄一、正則元字符1、 字符元字符2、重復(fù)元字符(量詞)3、定位元字符4、分組和替換字符5、特殊字符6、需要轉(zhuǎn)義的字符7、貪婪與非貪婪匹配8、常見正則表達式二、正則表達式應(yīng)用舉例1、驗證表達式vs中批量操作三、文件夾中的文件內(nèi)容正則批量替換1、使用VSCode文件替換(使用JS引擎)2、文件夾中的文件內(nèi)容正則批量替換一
這篇文章主要為大家詳細介紹了js實現(xiàn)支付倒計時返回首頁,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
HTML5中的大部分動畫都是通過Canvas實現(xiàn),因為Canvas就像一塊畫布,我們可以通過調(diào)用腳本在Canvas上繪制任意形狀,甚至是制作動畫,這篇文章主要給大家介紹了關(guān)于用幾十行js實現(xiàn)很炫的canvas交互特效的相關(guān)資料,需要的朋友可以參考下
這篇文章主要為大家詳細介紹了jquery插件實現(xiàn)懸浮的菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
這篇文章我們來了解用jQuery如何獲取ul下li的個數(shù),下文示例對新手學(xué)習(xí)jQuery有一定的參考價值,有這方面學(xué)習(xí)需要的朋友接下來就跟隨小編來一起學(xué)習(xí)一下吧!
推薦內(nèi)容
成為群英會員,開啟智能安全云計算之旅
立即注冊關(guān)注或聯(lián)系群英網(wǎng)絡(luò)
7x24小時售前:400-678-4567
7x24小時售后:0668-2555666
24小時QQ客服
群英微信公眾號
CNNIC域名投訴舉報處理平臺
服務(wù)電話:010-58813000
服務(wù)郵箱:[email protected]
投訴與建議:0668-2555555
Copyright ? QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版權(quán)所有
增值電信經(jīng)營許可證 : B1.B2-20140078 ICP核準(zhǔn)(ICP備案)粵ICP備09006778號 域名注冊商資質(zhì) 粵 D3.1-20240008