首页 > 其他分享 >Vue.js 独享路由守卫

Vue.js 独享路由守卫

时间:2023-04-13 10:46:50浏览次数:52  
标签:Vue title next meta props import js 路由

视频

  1. 独享守卫:
beforeEnter(to,from,next){
	console.log('beforeEnter',to,from)
	if(to.meta.isAuth){ //判断当前路由是否需要进行权限控制
		if(localStorage.getItem('school') === 'atguigu'){
			next()
		}else{
			alert('暂无权限查看')
			// next({name:'guanyu'})
		}
	}else{
		next()
	}
}

index.js

// 该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router'
//引入组件
import About from '../pages/About'
import Home from '../pages/Home'
import News from '../pages/News'
import Message from '../pages/Message'
import Detail from '../pages/Detail'

//创建并暴露一个路由器
const router =  new VueRouter({
	routes:[
		{
			name:'guanyu',
			path:'/about',
			component:About,
			meta:{title:'关于'}
		},
		{
			name:'zhuye',
			path:'/home',
			component:Home,
			meta:{title:'主页'},
			children:[
				{
					name:'xinwen',
					path:'news',
					component:News,
					meta:{isAuth:true,title:'新闻'},
					beforeEnter: (to, from, next) => {
						console.log('独享路由守卫',to,from)
						if(to.meta.isAuth){ //判断是否需要鉴权
							if(localStorage.getItem('school')==='atguigu'){
								next()
							}else{
								alert('学校名不对,无权限查看!')
							}
						}else{
							next()
						}
					}
				},
				{
					name:'xiaoxi',
					path:'message',
					component:Message,
					meta:{isAuth:true,title:'消息'},
					children:[
						{
							name:'xiangqing',
							path:'detail',
							component:Detail,
							meta:{isAuth:true,title:'详情'},

							//props的第一种写法,值为对象,该对象中的所有key-value都会以props的形式传给Detail组件。
							// props:{a:1,b:'hello'}

							//props的第二种写法,值为布尔值,若布尔值为真,就会把该路由组件收到的所有params参数,以props的形式传给Detail组件。
							// props:true

							//props的第三种写法,值为函数
							props($route){
								return {
									id:$route.query.id,
									title:$route.query.title,
									a:1,
									b:'hello'
								}
							}

						}
					]
				}
			]
		}
	]
})

//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
/* router.beforeEach((to,from,next)=>{
	console.log('前置路由守卫',to,from)
	if(to.meta.isAuth){ //判断是否需要鉴权
		if(localStorage.getItem('school')==='atguigu'){
			next()
		}else{
			alert('学校名不对,无权限查看!')
		}
	}else{
		next()
	}
}) */

//全局后置路由守卫————初始化的时候被调用、每次路由切换之后被调用
router.afterEach((to,from)=>{
	console.log('后置路由守卫',to,from)
	document.title = to.meta.title || '硅谷系统'
})

export default router

标签:Vue,title,next,meta,props,import,js,路由
From: https://www.cnblogs.com/chuixulvcao/p/17312596.html

相关文章

  • 【BUG】ExtJS 的Tab Reorder 插件持续更新布局问题解决办法 (Solution to layout issue
    更新记录2023年4月13日初始化。ExtJS教程汇总:https://www.cnblogs.com/cqpanda/p/16328016.html问题不停的拖动tab栏,会不断更新布局。Draggingthetabbarcontinuouslywillupdatethelayoutconstantly.解决办法进入ExtJS包,打开ux目录下的BoxReorderer.js文件,找......
  • Vuex module之间调用与读值
    对于模块内部的mutation和getter,接收的第一个参数是模块的局部状态对象state对于模块内部的action,局部状态通过context.state暴露出来,根节点状态则为context.rootState使用全局state和getter,rootState和rootGetters会作为第三和第四参数传入getter,也会通过conte......
  • 基于Java+Springboot+vue网上商品订单转手系统设计和实现
    基于Java+Springboot+vue网上商品订单转手系统设计和实现一、前言介绍:1.1项目摘要传统办法管理信息首先需要花费的时间比较多,其次数据出错率比较高,而且对错误的数据进行更改也比较困难,最后,检索数据费事费力。因此,在计算机上安装网上商品订单转手系统软件来发挥其高效地信息处理......
  • vue中父子组件调用之头像更新
    问题描述:修改图像的功能,要实现的功能是这样的:点击确认按钮,然后连带着上面的头像也更新,是不是很简单,只需要一个刷新就行,但是事实上没这么简单,因为我的这个项目涉及到三个组件 这里面中间是一个子路由,然后上面是一个组件,左边是一个组件  然后这个就涉及到子父组件传值,这......
  • js 判断是否为 IE 通过事件关闭新打开的浏览器窗口
    //必须通过target="_blank"打开新窗口才可关闭if(window.ActiveXObject||"ActiveXObject"inwindow){//iewindow.location.href="about:blank";//为兼容IE作此修改window.close();}else{wi......
  • vscode如何设置HTML/CSS/JS保存后自动格式化?
    具体操作:点击小齿轮,选择设置按钮  第二步:点击右上角按钮,进入设置页面  第三步:粘贴自动保存的js代码!  "editor.formatOnType":true,"editor.formatOnSave":true, ......
  • 好奇心驱使下试验了 chatGPT 的 js 代码的能力
    手边的项目中有个函数,主要实现图片分片裁剪功能。可以优化一下。也想看看chatGPT的代码理解能力,优化能力,实现能力,用例能力。于是有了这篇文章。实验结果总结:chatGPT确实强大,提供的答案可以借鉴,但不能完全依赖,需要你自行判断是否正确当你需要一个纯函数时,可以考虑使用。......
  • js字符串转base64
    js字符串转base64原文链接:https://blog.csdn.net/qq_40666120/article/details/120146906字符串转base64functionencode(str){ //对字符串进行编码 varencode=encodeURI(str); //对编码的字符串转化base64 varbase64=btoa(encode); returnbase64;}1234567......
  • JavaScript 数组字符串转换Json格式
    JavaScript数组字符串转换Json格式//滔Roy2023.04.13functionparseStringToArray(str){//尝试将字符串解析为JSON格式try{constarr=JSON.parse(str);//如果解析成功,则直接返回解析结果if(Array.isArray(arr)){returnarr;}}......
  • JS字符串转base64格式
    JS字符串转base64格式原文链接:https://www.cnblogs.com/liu-fei-fei/p/7251105.htmlvarBase64={//privateproperty_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",//publicmethodforencodingencode:functi......