手把手教你vue實(shí)現(xiàn)動(dòng)態(tài)路由
1、什么是動(dòng)態(tài)路由?
動(dòng)態(tài)路由,動(dòng)態(tài)即不是寫死的,是可變的。我們可以根據(jù)自己不同的需求加載不同的路由,做到不同的實(shí)現(xiàn)及頁面的渲染。動(dòng)態(tài)的路由存儲(chǔ)可分為兩種,一種是將路由存儲(chǔ)到前端。另一種則是將路由存儲(chǔ)到數(shù)據(jù)庫。動(dòng)態(tài)路由的使用一般結(jié)合角色權(quán)限控制一起使用。
總結(jié):
1)路由可變,不是寫死的,動(dòng)態(tài)加載
2)存儲(chǔ)分兩種:存前端,存數(shù)據(jù)庫
2、動(dòng)態(tài)路由的好處
使用動(dòng)態(tài)路由可以跟靈活,無需手工維護(hù),我們可以使用一個(gè)頁面對(duì)路由進(jìn)行維護(hù)。如果將路由存儲(chǔ)到數(shù)據(jù)庫,還可以增加安全性。
總結(jié):
1)靈活,無需手工維護(hù)
2)增加安全性
3、動(dòng)態(tài)路由如何實(shí)現(xiàn)
在此以路由存儲(chǔ)在數(shù)據(jù)庫為例
流程:一般我們?cè)诘卿浀臅r(shí)候,根據(jù)登錄用戶的角色返回此角色可以訪問的頁面的路由,前端將路由存儲(chǔ)到vuex(vuex存儲(chǔ)的數(shù)據(jù)必須可持久的,不要一刷新頁面就不見),我們?cè)诼酚汕爸檬匦l(wèi)處動(dòng)態(tài)添加拿到的路由,對(duì)頁面進(jìn)行渲染。
1)此為我的router目錄,index.js對(duì)路由添加,守衛(wèi)攔截等處理。static-route.js為前端定義的靜態(tài)路由,不需要?jiǎng)討B(tài)加載的,如登陸頁面,忘記密碼頁面,404頁面等。
index.js
import Vue from 'vue' import $cookies from 'vue-cookies' import VueRouter from 'vue-router' import store from '../store'? import staticRoute from './static-route.js'??? Vue.use(VueRouter)?? const router = new VueRouter({ mode: 'history', base: process.env.BASE_URL, routes: staticRoute //staticRoute為靜態(tài)路由,不需動(dòng)態(tài)添加 })? let isToken = true router.beforeEach(async (to, from, next) => { //定義isToken為true和vuex不為空時(shí)添加路由 if (isToken && store.state.routers.routers.length != 0) { //從vuex中獲取動(dòng)態(tài)路由 const accessRouteses = await store.state.routers.routers; //動(dòng)態(tài)路由循環(huán)解析和添加 accessRouteses.forEach(v => { v.children = routerChildren(v.children); v.component = routerCom(v.component); router.addRoute(v); //添加 }) isToken = false //將isToken賦為 false ,否則會(huì)一直循環(huán),崩潰 next({ ...to, // next({ ...to })的目的,是保證路由添加完了再進(jìn)入頁面 (可以理解為重進(jìn)一次) replace: true, // 重進(jìn)一次, 不保留重復(fù)歷史 })? } else { if (to.name == null) { next("/404") } else { if (to.meta.title) { //判斷是否有標(biāo)題 document.title = to.meta.title //給相應(yīng)頁面添加標(biāo)題 } next() }? }? })? function routerCom(path) { //對(duì)路由的component解析 return (resolve) => require([`@/views/${path}`], resolve); }? function routerChildren(children) { //對(duì)子路由的component解析 children.forEach(v => { v.component = routerCom(v.component); if (v.children != undefined) { v.children = routerChildren(v.children) } }) return children }?? export default router?
2)登陸成功后將獲取到的動(dòng)態(tài)路由存儲(chǔ)到vuex
vuex—>index.js
import Vue from 'vue' import Vuex from 'vuex' //數(shù)據(jù)持久化 import createPersistedState from "vuex-persistedstate"; ? Vue.use(Vuex) const routers = { namespaced: true, state: () => ({ routers:"", }), mutations: { routers(state, newsdata) { state.routers = newsdata }, ? }, actions: { routers(context) { context.commit('routers') }, }, getters: { routers(state) { console.log("getters", state) return state.routers }, } } ? ? const store = new Vuex.Store({ modules: { routers: routers, }, // 數(shù)據(jù)持久化 plugins: [createPersistedState({ //key是存儲(chǔ)數(shù)據(jù)的鍵名 key: 'routersData', //paths是存儲(chǔ)state中的那些數(shù)據(jù),如果是模塊下具體的數(shù)據(jù)需要加上模塊名稱,如user.token paths: ["routers.routers"] })] }) ? ? export default store
我的動(dòng)態(tài)路由模板
//動(dòng)態(tài)路由 const dynamicRoute = [{ "path": "/main", "name": "main", "redirect": "/main/index", "component": "main/main.vue", "children": [{ "path": "index", "name": "index", "component": "index/index.vue", "meta": { "name": "index", "title": "首頁", "icon": "el-icon-location", "menu":true //true為菜單欄 } }, { "path": "Configuration", "name": "Configuration", "redirect": "Configuration/route", "component": "Configuration/index.vue", "roles": ['developer', "admin"], // developer、admin角色的用戶才能訪問該頁面 "meta": { "title": "配置", "icon": "el-icon-location", "menu":true }, "children": [{ "path": "route", "name": "route", "component": "Configuration/route/index.vue", "meta": { "title": "菜單", "icon": "", "menu":true }, }, { "path": "user", "name": "user", "component": "Configuration/user/index.vue", "meta": { "title": "用戶管理", "icon": "el-icon-location", "menu":true }, }, { "path": "admin", "name": "admin", "component": "Configuration/admin/index.vue", "meta": { "title": "管理員管理", "icon": "", "menu":true }, }, { "path": "userEdit", "name": "userEdit", "component": "Configuration/user/user-Edit.vue", "meta": { "title": "編輯用戶", "icon": "", "menu":false }, }, ] }, { "path": "check", "name": "check", "redirect": "check/user", "component": "check/index.vue", "roles": ['developer', "admin", "check"], // developer、admin角色的用戶才能訪問該頁面 "meta": { "title": "審核", "icon": "el-icon-location", "menu":true }, "children": [{ "path": "user", "name": "checkUser", "component": "check/check-user/index.vue", "meta": { "title": "用戶實(shí)名審核", "icon": "el-icon-location", "menu":true } }, { "path": "enterprise", "name": "checkEnterprise", "component": "check/check-enterprise/index.vue", "meta": { "title": "企業(yè)認(rèn)證審核", "icon": "el-icon-location", "menu":true }, }, { "path": "checkNormImage", "name": "checkNormImage", "component": "check/check-norm-image/index.vue", "meta": { "title": "標(biāo)準(zhǔn)照認(rèn)證審核", "icon": "el-icon-location", "menu":true }, }, { "path": "checkHiringJobs", "name": "checkHiringJobs", "component": "check/check-hiring-Jobs/index.vue", "meta": { "title": "求職、招聘認(rèn)證審核", "icon": "el-icon-location", "menu":true }, } ] ? } ] }, ] export default dynamicRoute
路由管理界面(可能有不完善的地方)
講一講遇到的坑及注意點(diǎn)
1)“component”: “check/check-norm-image/index.vue”, 用字符串再在解析,不要像靜態(tài)路由一樣。否則第一次進(jìn)去可以,刷新就變空白
2)此處為重要的一點(diǎn),直接用next()不行
next({ ...to, // next({ ...to })的目的,是保證路由添加完了再進(jìn)入頁面 (可以理解為重進(jìn)一次) replace: true, // 重進(jìn)一次, 不保留重復(fù)歷史 })
3)由于添加完路由還會(huì)重復(fù)執(zhí)行一遍路由守衛(wèi),所有必須確保不要一直死循環(huán)添加路由。否則直接崩潰。這里我用的是isToken變量確保不循環(huán)。
總結(jié)
到此這篇關(guān)于vue實(shí)現(xiàn)動(dòng)態(tài)路由的文章就介紹到這了,更多相關(guān)vue實(shí)現(xiàn)動(dòng)態(tài)路由內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中v-model動(dòng)態(tài)生成的實(shí)例詳解
這篇文章主要介紹了vue中v-model動(dòng)態(tài)生成的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10Vue移動(dòng)端用淘寶彈性布局lib-flexible插件做適配的方法
這篇文章主要介紹了Vue移動(dòng)端用淘寶彈性布局lib-flexible插件做適配的操作方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05遇到vue前端npm?i報(bào)錯(cuò)多個(gè)版本不一致問題及解決
這篇文章主要介紹了遇到vue前端npm?i報(bào)錯(cuò)多個(gè)版本不一致問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03詳解vuex中mapState,mapGetters,mapMutations,mapActions的作用
這篇文章主要介紹了vuex中mapState,mapGetters,mapMutations,mapActions的作用,需要的朋友可以參考下2018-04-04在vue3中使用el-tree-select實(shí)現(xiàn)樹形下拉選擇器效果
el-tree-select是一個(gè)含有下拉菜單的樹形選擇器,結(jié)合了?el-tree?和?el-select?兩個(gè)組件的功能,這篇文章主要介紹了在vue3中使用el-tree-select做一個(gè)樹形下拉選擇器,需要的朋友可以參考下2024-03-03vue動(dòng)態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果
這篇文章主要為大家詳細(xì)介紹了vue動(dòng)態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03如何在基于vue-cli的項(xiàng)目自定義打包環(huán)境
本篇文章主要介紹了在基于vue-cli的項(xiàng)目自定義打包環(huán)境,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11Vue之使用mockjs生成模擬數(shù)據(jù)案例詳解
這篇文章主要介紹了Vue之使用mockjs生成模擬數(shù)據(jù)案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09