路由导航分析和三后台框架路由导航横向对比
创始人
2024-06-03 06:53:53
0

博主菜鸡,有错请各位指出

0、路由导航是什么

路由守卫

router.beforeEach:全局前置守卫。
router.beforeResolve:全局解析守卫。
router.afterEach:全局后置钩子。

组件守卫

beforeRouteEnter
beforeRouteUpdate
beforeRouteLeave

路由独享守卫

beforeEnter

总结
导航被触发。
在失活的组件里调用 beforeRouteLeave 守卫。
调用全局的 beforeEach 守卫。
在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+)。
在路由配置里调用 beforeEnter
解析异步路由组件。
在被激活的组件里调用 beforeRouteEnter
调用全局的 beforeResolve 守卫 (2.5+)。
导航被确认。
调用全局的 afterEach 钩子。
触发 DOM 更新。
调用 beforeRouteEnter守卫中传给 next 的回调函数,创建好的组件实例会作为回调函数的参数传入。

全局守卫

router.beforeEach((to,from,next)=>{})

router.beforeEach((to,from,next)=>{if(to.path == '/login' || to.path == '/register'){next();}else{alert('您还没有登录,请先登录');next('/login');}
})

router.afterEach((to,from)=>{})
只有两个参数,to:进入到哪个路由去,from:从哪个路由离。

组件内守卫

到达组件时
beforeRouteEnter:(to,from,next)=>{}


离开组件时
beforeRouteLeave:(to,from,next)=>{}
点击其他组件时,判断是否确认离开。确认执行next();取消执行next(false),留在当前页面。

beforeRouteLeave:(to,from,next)=>{if(confirm("确定离开此页面吗?") == true){next();}else{next(false);}}

组件更新时
beforeRouteUpdate:(to,from,next)=>{}

  • 当组件内子路由发生变化时,会出发该导航守卫。
  • 当使用 beforeRouteUpdate 导航守卫时,应该等 next() 函数执行后,再获取 params 或 query 中的参数。
  beforeRouteUpdate (to, from, next) {console.log('路由更新之前:从to获取参数', to.params, '从this.$route获取参数', this.$route.params)next()console.log('路由更新之后:从to获取参数', to.params, '从this.$route获取参数', this.$route.params)},

路由独享守卫

beforeEnter:(to,from,next)=>{}
写进其中一个路由对象中,只在这个路由下起作用。

{path:"/login",name:"login",component:"/login",beforeEnter:(to,from,next)=>{next('/login')}
}

1、el-admin

1.1、router.afterEach:全局后置钩子

进度条结束

NProgress.done()

1.2、router.beforeEach:全局前置守卫

index.js

import router from './routers'
import store from '@/store'
import Config from '@/settings'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css'// progress bar style
import { getToken } from '@/utils/auth' // getToken from cookie
import { buildMenus } from '@/api/system/menu'
import { filterAsyncRouter } from '@/store/modules/permission'NProgress.configure({ showSpinner: false })// NProgress Configurationconst whiteList = ['/login']// no redirect whitelist
router.beforeEach((to, from, next) => {if (to.meta.title) {//假如路由组件挂载的时候有写title,则是title-项目名称,Config.title配置了项目名称document.title = to.meta.title + ' - ' + Config.title}NProgress.start()//开启进度条if (getToken()) {if (to.path === '/login') {// 已登录且要跳转的页面是登录页,此时不能进登录页next({ path: '/' })//修改路由为loginNProgress.done()} else {//这块Vuex没看懂if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息store.dispatch('GetInfo').then(() => { // 拉取user_info// 动态路由,拉取菜单loadMenus(next, to)}).catch(() => {store.dispatch('LogOut').then(() => {location.reload() // 为了重新实例化vue-router对象 避免bug})})// 登录时未拉取 菜单,在此处拉取} else if (store.getters.loadMenus) {// 修改成false,防止死循环store.dispatch('updateLoadMenus')loadMenus(next, to)} else {next()//不满足其他条件,放行}}} else {/* has no token*/if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入next()} else {next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页NProgress.done()}}
})export const loadMenus = (next, to) => {buildMenus().then(res => {const sdata = JSON.parse(JSON.stringify(res))const rdata = JSON.parse(JSON.stringify(res))const sidebarRoutes = filterAsyncRouter(sdata)const rewriteRoutes = filterAsyncRouter(rdata, false, true)rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })store.dispatch('GenerateRoutes', rewriteRoutes).then(() => { // 存储路由router.addRoutes(rewriteRoutes) // 动态添加可访问路由表next({ ...to, replace: true })})store.dispatch('SetSidebarRouters', sidebarRoutes)})
}

permission.js

import { constantRouterMap } from '@/router/routers'
import Layout from '@/layout/index'
import ParentView from '@/components/ParentView'const permission = {state: {routers: constantRouterMap,addRouters: [],sidebarRouters: []},mutations: {SET_ROUTERS: (state, routers) => {state.addRouters = routersstate.routers = constantRouterMap.concat(routers)},SET_SIDEBAR_ROUTERS: (state, routers) => {state.sidebarRouters = constantRouterMap.concat(routers)}},actions: {GenerateRoutes({ commit }, asyncRouter) {commit('SET_ROUTERS', asyncRouter)},SetSidebarRouters({ commit }, sidebarRouter) {commit('SET_SIDEBAR_ROUTERS', sidebarRouter)}}
}export const filterAsyncRouter = (routers, lastRouter = false, type = false) => { // 遍历后台传来的路由字符串,转换为组件对象return routers.filter(router => {if (type && router.children) {router.children = filterChildren(router.children)}if (router.component) {if (router.component === 'Layout') { // Layout组件特殊处理router.component = Layout} else if (router.component === 'ParentView') {router.component = ParentView} else {const component = router.componentrouter.component = loadView(component)}}if (router.children != null && router.children && router.children.length) {router.children = filterAsyncRouter(router.children, router, type)} else {delete router['children']delete router['redirect']}return true})
}function filterChildren(childrenMap, lastRouter = false) {var children = []childrenMap.forEach((el, index) => {if (el.children && el.children.length) {if (el.component === 'ParentView') {el.children.forEach(c => {c.path = el.path + '/' + c.pathif (c.children && c.children.length) {children = children.concat(filterChildren(c.children, c))return}children.push(c)})return}}if (lastRouter) {el.path = lastRouter.path + '/' + el.path}children = children.concat(el)})return children
}export const loadView = (view) => {return (resolve) => require([`@/views/${view}`], resolve)
}
export default permission

2、Vue-beautiful-admin

3、Blade-X

相关内容

热门资讯

Python|位运算|数组|动... 目录 1、只出现一次的数字(位运算,数组) 示例 选项代...
张岱的人物生平 张岱的人物生平张岱(414年-484年),字景山,吴郡吴县(今江苏苏州)人。南朝齐大臣。祖父张敞,东...
西游西后传演员女人物 西游西后传演员女人物西游西后传演员女人物 孙悟空 六小龄童 唐僧 徐少华 ...
名人故事中贾岛作诗内容简介 名人故事中贾岛作诗内容简介有一次,贾岛骑驴闯了官道.他正琢磨着一句诗,名叫《题李凝幽居》全诗如下:闲...
和男朋友一起优秀的文案? 和男朋友一起优秀的文案?1.希望是惟一所有的人都共同享有的好处;一无所有的人,仍拥有希望。2.生活,...
戴玉手镯的好处 戴玉手镯好还是... 戴玉手镯的好处 戴玉手镯好还是碧玺好 女人戴玉?戴玉好还是碧玺好点佩戴手镯,以和田玉手镯为佳!相嫌滑...
依然什么意思? 依然什么意思?依然(汉语词语)依然,汉语词汇。拼音:yī    rán基本解释:副词,指照往常、依旧...
高尔基的散文诗 高尔基的散文诗《海燕》、《大学》、《母亲》、《童年》这些都是比较出名的一些代表作。
心在飞扬作者简介 心在飞扬作者简介心在飞扬作者简介如下。根据相关公开资料查询,心在飞扬是一位优秀的小说作者,他的小说作...
卡什坦卡的故事赏析? 卡什坦卡的故事赏析?讲了一只小狗的故事, 我也是近来才读到这篇小说. 作家对动物的拟人描写真是惟妙...
林绍涛为简艾拿绿豆糕是哪一集 林绍涛为简艾拿绿豆糕是哪一集第三十二集。 贾宽认为是阎帅间接导致刘映霞住了院,第二天上班,他按捺不...
小爱同学是女生吗小安同学什么意... 小爱同学是女生吗小安同学什么意思 小爱同学,小安同学说你是女生。小安是男的。
内分泌失调导致脸上长斑,怎么调... 内分泌失调导致脸上长斑,怎么调理内分泌失调导致脸上长斑,怎么调理先调理内分泌,去看中医吧,另外用好的...
《魔幻仙境》刺客,骑士人物属性... 《魔幻仙境》刺客,骑士人物属性加点魔幻仙境骑士2功1体质
很喜欢她,该怎么办? 很喜欢她,该怎么办?太冷静了!! 太理智了!爱情是需要冲劲的~不要考虑着考虑那~否则缘...
言情小说作家 言情小说作家我比较喜欢匪我思存的,很虐,很悲,还有梅子黄时雨,笙离,叶萱,还有安宁的《温暖的玄》 小...
两个以名人的名字命名的风景名胜... 两个以名人的名字命名的风景名胜?快太白楼,李白。尚志公园,赵尚志。
幼儿教育的代表人物及其著作 幼儿教育的代表人物及其著作卡尔威特的《卡尔威特的教育》,小卡尔威特,他儿子成了天才后写的《小卡尔威特...
海贼王中为什么说路飞打凯多靠霸... 海贼王中为什么说路飞打凯多靠霸气升级?凯多是靠霸气升级吗?因为之前刚到时确实打不过人家因为路飞的实力...
运气不好拜财神有用吗运气不好拜... 运气不好拜财神有用吗运气不好拜财神有没有用1、运气不好拜财神有用。2、拜财神上香前先点蜡烛,照亮人神...