vue——路由守卫

一、全局路由守卫

在export路由之前配置beforeEach((to,from,next) => {})

const router = new VueRouter({
	routes: [
		……
	]
})
// 全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to,from,next) => {	//to为目的路由信息 from为起始路由信息
	console.log(to,from)
	//可在此添加一些限制条件
	next()	//放行
})
export default router

可在路由的meta中配置是否需要校验

routes: [
	{
		name: 'home',
		path: '/home',
		component: MyHome,
		meta: {isAuth: true},
	}
]

router.beforeEach((to,from,next) => {	//to为目的路由信息 from为起始路由信息
	if(to.meta.isAuth)
		alert("hello")
	else{
		next()
	}
	console.log(to,from)
})

后置路由守卫afterEach((to,from) => {}) 做一些跳转成功后的操作 比如:修改标签页标题

routes: [
	{
		name: 'home',
		path: '/home',
		component: MyHome,
		meta: {isAuth: true,title:'hello word'},
	}
]

router.afterEach((to,from) => {
	console.log("后置路由")
	document.title = to.meta.title
})

二、独享路由守卫

配置路由时直接配置beforeEnter((to,from,next) => {})
独享路由只有前置路由
例如

routes: [
	{
		name: 'about',
		path: '/about',
		component: MyAbout,
		beforeEnter((to,from,next) => {	//to为目的路由信息 from为起始路由信息
			console.log(to,from,next)
		}
	},
]

三、组件内路由

配置在组件文件里
beforeRouteEnter(to,from,next) 通过路由规则,进入该组件时调用
beforeRouteLeave(to,from,next) 通过路由规则,离开该组件时被调用
例如

<script>
	export default {
		name: 'MyAbout',
		// 通过路由规则,进入该组件时调用
		beforeRouteEnter(to,from,next) {
			console.log(to,from)
			next()
		},
		// 通过路由规则,离开该组件时被调用
		beforeRouteLeave(to,from,next) {
			console.log(to,from)
			next()
		}
	}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,我们可以使用前置路由守卫来在切换到某个路由组件之前执行一些操作。在main.js入口文件中,我们可以添加以下代码来设置前置路由守卫: ```javascript router.beforeEach((to, from, next) => { // to 目标路由 // from 来源 // next 放行 // 如果需要验证,首页守卫 if (to.meta.requireAuth) { // vuex仓库中的信息是否存在 if (store.getters.user.userName) { next(); } else { // 拦截路由 next('./login'); } } else { // 不需要验证,直接放行 next(); } // 登陆、注册守卫 if (to.meta.cheakIsLogin) { if (store.getters.user.userName) { // next('/'); // 路由让其返回至原来的地址 router.back(); } else { next(); } } else { next(); } }); ``` 这段代码中,我们使用`beforeEach`方法来定义前置路由守卫。在该方法中,我们可以根据需要对目标路由进行验证,判断是否需要进行权限验证或者是否需要检查用户是否已登录。根据验证结果,我们可以选择放行或者拦截路由。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue——前置路由守卫](https://blog.csdn.net/YUHUI01/article/details/84201043)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Vue中的路由守卫](https://blog.csdn.net/weixin_47075145/article/details/127336962)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值