Vue_Router_守卫

路由守卫:路由进行权限控制。

分为:全局守卫,独享守卫,组件内守卫。

全局守卫

//创建并暴露 路由器
const router=new Vrouter({
    mode:"hash"//"hash路径出现#但是兼容性强,history没有#兼容性差"
    routes:[
        {
            name:'banji',//命名路由,:to="{name='banji'}"
            path:'/class',  //当路径是 /class时,内容区就显示ClassPage这个组件。
            component:ClassPage,//上面的引用
			meta:{title:'信息'}//{}中自定义信息
        },
        {
            path:'/student',
            component:StudentPage,
			children:[//嵌套路由
			  {
				name:'banji',
                path:'/class',
                component:ClassPage,
			    meta:{title:'信息'}
			  }
			]
        }
    ]
});
//暴露之前添加守卫

//全局的路由前置守卫,处理可否去到目标组件。to,form 都有详细的【去来】信息
router.beforeEach((to,from,next)=>{
     //to:去哪,
	 //from:从哪来
	 document.title=from.meta.title;//读信息
	 //next()//放行,让不让跳转
})

//全局的路由后置守卫,处理到达组件之后的操作
router.afterEach((to,from)=>{
     //to:去哪,
	 //from:从哪来
})

//暴露路由
export default router;

独享守卫

//创建并暴露 路由器
const router=new Vrouter({
    routes:[
        {
            path:'/student',
            component:StudentPage,
			children:[//嵌套路由
			  {
				....
			  }
			],
			beforeEnter:(to,from,next)=>{...}//****独享路由守卫,里面与全局一样
        }
    ]
});
//暴露路由
export default router;

组件内守卫

//组件里面的路由守卫
export default {
     name:'组件名',
     data(){
         return{属性:值}
     },
     props:['参数'],
	 //***进入组件时的守卫,通过路由规则进入的,作用有点类似全局守卫。to,form 都有详细的【去来】信息
	 router.beforeEach((to,from,next)=>{
		 //to:去哪,
		 //from:从哪来
		 document.title=from.meta.title;//读信息
		 //next()允许进来
	 })

	 //离开组件时的守卫,处理到达组件之后的操作
	 router.afterEach((to,from,next)=>{
		 //to:去哪,
		 //from:从哪来
		 //next()允许出去
	 })
}

相关推荐

  1. VueRouter的编程式导航和导航守卫

    2024-02-01 03:26:01       35 阅读
  2. VueRouter

    2024-02-01 03:26:01       39 阅读
  3. <span style='color:red;'>VueRouter</span>

    VueRouter

    2024-02-01 03:26:01      13 阅读
  4. 012vuerouter

    2024-02-01 03:26:01       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-01 03:26:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-01 03:26:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-01 03:26:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-01 03:26:01       18 阅读

热门阅读

  1. 【python】积分

    2024-02-01 03:26:01       47 阅读
  2. MicroPython核心:用C扩展MicroPython

    2024-02-01 03:26:01       34 阅读
  3. 得物开放平台接入得物SDK

    2024-02-01 03:26:01       31 阅读
  4. Vue3中的watch函数使用

    2024-02-01 03:26:01       35 阅读
  5. 【前端】日期转换

    2024-02-01 03:26:01       31 阅读
  6. oracle 监听的主机名出现异常时候,排查放向

    2024-02-01 03:26:01       37 阅读
  7. 关于我用AI编写了一个聊天机器人……(8)

    2024-02-01 03:26:01       38 阅读