vue3動(dòng)態(tài)路由addRoute實(shí)例詳解
Vue2中,有兩種方法實(shí)現(xiàn)路由權(quán)限動(dòng)態(tài)渲染:
router.addRoute(parentOrRoute, route) //添加單個(gè) router.addRoutes(routes) //添加多個(gè)
但在Vue3中,只保留了 addRoute()
方法。
首先,假設(shè)路由如下:
const menu = [{ id: 'system-manage', name: 'system-manage', path: '/system', meta:{ title: '系統(tǒng)管理', icon: 'setting', }, compoent: 'layout', children: [ { id: 'role', name: 'role', path: '/system/role', meta:{ title: '角色管理', icon: 'user-filled', }, compoent: 'view/system/role', children: [] } ] }]
// 遞歸替換引入component function dynamicRouter(routers) { const list = [] routers.forEach((itemRouter,index) => { list.push({ ...itemRouter, component: ()=>import(`@/${itemRouter.component}`) }) // 是否存在子集 if (itemRouter.children && itemRouter.children.length) { list[index].children = dynamicRouter(itemRouter.children); } }) return list } // 防止首次或者刷新界面路由失效 let registerRouteFresh = true router.beforeEach((to, from, next) => { if (registerRouteFresh) { // addRoute允許帶children添加,所以循環(huán)第一層即可 dynamicRouter(menu).forEach((itemRouter) => { router.addRoute(itemRouter) }) next({ ...to, replace: true }) registerRouteFresh = false } else { next() } })
使用這種拼接的方式會(huì)導(dǎo)致全局scss多次注入錯(cuò)誤,而且需要后臺(tái)返回文件路徑。
所以改用以下方式:
// 在本地建一個(gè)路由表 const path = [{ path: '/system/role', name: '角色管理', component: () => import('@/views/system/role') }] function dynamicRouter(routers) { const list = [] routers.forEach((itemRouter,index) => { // 從本地找組件 const authRoute = path.find(i=>i.path==itemRouter.path) list.push({ ...itemRouter, component: authRoute?.component || Layout //沒(méi)找到則用默認(rèn)框架組件 }) // 是否存在子集 if (itemRouter.children && itemRouter.children.length) { list[index].children = dynamicRouter(itemRouter.children); } }) return list }
如果出現(xiàn) Error: Invalid route component
,看下路徑是否正確,還有 addRoute
里傳的是否是對(duì)象,一開始傳了數(shù)組導(dǎo)致找了半天(扶額
以上。
到此這篇關(guān)于vue3動(dòng)態(tài)路由addRoute的文章就介紹到這了,更多相關(guān)vue3動(dòng)態(tài)路由addRoute內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3+ant?design的form數(shù)組表單校驗(yàn)方法
這篇文章主要介紹了vue3+ant?design的form數(shù)組表單,如何校驗(yàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-09-09vue 接口請(qǐng)求地址前綴本地開發(fā)和線上開發(fā)設(shè)置方式
這篇文章主要介紹了vue 接口請(qǐng)求地址前綴本地開發(fā)和線上開發(fā)設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Vue中ref、reactive、toRef、toRefs、$refs的基本用法總結(jié)
這篇文章主要給大家介紹了關(guān)于Vue中ref、reactive、toRef、toRefs、$refs的基本用法,以及他們之家的區(qū)別,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11Vue實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求攔截
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求攔截,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10vue 使用vant插件做tabs切換和無(wú)限加載功能的實(shí)現(xiàn)
這篇文章主要介紹了vue 使用vant插件做tabs切換和無(wú)限加載功能的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11vue移動(dòng)端項(xiàng)目vant組件庫(kù)之tag詳解
這篇文章主要介紹了vue移動(dòng)端項(xiàng)目vant組件庫(kù)之tag詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04ElementPlus?Table表格實(shí)現(xiàn)可編輯單元格
本文主要介紹了ElementPlus?Table表格實(shí)現(xiàn)可編輯單元格,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12