Vue-router:10、路由守卫

2024-04-10 16:32

本文主要是介绍Vue-router:10、路由守卫,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Vue-router:10、路由守卫

Vue-router中的路由守卫,主要是对其内容进行保护,如果没有对应的权限,则不允许访问。

我们首先来看一下全局守卫,也就是所有的路由都会经过全局守卫来进行检测。

  //实现全局守卫router.beforeEach((to, from, next) => {//to:去哪个页面,from来自哪个页面,next继续执行.//判断哪个路由需要进行守卫,这里可以通过元数据方式if (to.meta.auth) {if (window.isLogin) {next();} else {next("/login?redirect=" + to.fullPath);}} else {next();}});

在上面的代码中,创建了路由守卫,但是需要判断的是需要对哪个路由进行守卫,这里就是通过元数据来进行判断的。如果所跳转到的路由有元数据,并且对应的auth属性为true表明是需要进行守卫的,那么下面就需要校验用户是否登录,这里是通过判断否window.isLogin的值是否为true来进行判断的(这里简化了操作,实际应用中应该存储到sessionStorage),如果条件成立则表明用户登录,就继续访问用户希望访问到的页面,否则跳转到登录页面,而且将用户希望访问的页面地址也传递到了登录页面,这样用户登录成功后,可以直接跳转到要访问的页面。

如果没有元数据,则继续访问用户要访问的页面。

 // 创建路由对象const router = new VueRouter({routes: [{ path: "/login", component: Login },{path: "/",component: App,redirect: "/users",children: [{path: "/users",component: Users,meta: {auth: true,},},{ path: "/userinfo/:id", component: UserInfo, props: true },{ path: "/rights", component: Rights },{ path: "/goods", component: Goods },{ path: "/orders", component: Orders },{ path: "/settings", component: Settings },],},],});

在上面的代码中,给/users路由添加了元数据。

登录组件创建如下:

  const Login = {data() {return {isLogin: window.isLogin,};},template: `<div><button @click="login" v-if="!isLogin">登录</button><button @click="logout" v-else>注销</button></div>`,methods: {login() {window.isLogin = true;this.$router.push(this.$route.query.redirect);},logout() {this.isLogin = window.isLogin = false;},},};

当单击登录按钮后,进行将window.isLogin设置为true, 并且进行跳转。

全部代码如下

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>基于vue-router的案例</title><script src="./lib/vue.js"></script><script src="https://unpkg.com/vue-router/dist/vue-router.js"></script><style type="text/css">html,body,#app {margin: 0;padding: 0px;height: 100%;}.header {height: 50px;background-color: #545c64;line-height: 50px;text-align: center;font-size: 24px;color: #fff;}.footer {height: 40px;line-height: 40px;background-color: #888;position: absolute;bottom: 0;width: 100%;text-align: center;color: #fff;}.main {display: flex;position: absolute;top: 50px;bottom: 40px;width: 100%;}.content {flex: 1;text-align: center;height: 100%;}.left {flex: 0 0 20%;background-color: #545c64;}.left a {color: white;text-decoration: none;}.right {margin: 5px;}.btns {width: 100%;height: 35px;line-height: 35px;background-color: #f5f5f5;text-align: left;padding-left: 10px;box-sizing: border-box;}button {height: 30px;background-color: #ecf5ff;border: 1px solid lightskyblue;font-size: 12px;padding: 0 20px;}.main-content {margin-top: 10px;}ul {margin: 0;padding: 0;list-style: none;}ul li {height: 45px;line-height: 45px;background-color: #a0a0a0;color: #fff;cursor: pointer;border-bottom: 1px solid #fff;}table {width: 100%;border-collapse: collapse;}td,th {border: 1px solid #eee;line-height: 35px;font-size: 12px;}th {background-color: #ddd;}</style></head><body><div id="app"><router-view></router-view></div><script>const App = {template: `<div><!-- 头部区域 --><header class="header">传智后台管理系统</header><!-- 中间主体区域 --><div class="main"><!-- 左侧菜单栏 --><div class="content left"><ul><li><router-link to="/users"> 用户管理</router-link></li><li><router-link to="/rights"> 权限管理</router-link></li><li><router-link to="/goods"> 商品管理</router-link></li><li><router-link to="/orders"> 订单管理</router-link></li><li><router-link to="/settings"> 系统设置</router-link></li></ul></div><!-- 右侧内容区域 --><div class="content right"><div class="main-content"> <router-view /></div></div></div><!-- 尾部区域 --><footer class="footer">版权信息</footer></div>`,};const Users = {data() {return {userlist: [{ id: 1, name: "张三", age: 10 },{ id: 2, name: "李四", age: 20 },{ id: 3, name: "王五", age: 30 },{ id: 4, name: "赵六", age: 40 },],};},methods: {goDetail(id) {console.log(id);this.$router.push("/userinfo/" + id);},},template: `<div><h3>用户管理区域</h3><table><thead><tr><th>编号</th><th>姓名</th><th>年龄</th><th>操作</th></tr></thead><tbody><tr v-for="item in userlist" :key="item.id"><td>{{item.id}}</td><td>{{item.name}}</td><td>{{item.age}}</td><td><a href="javascript:;" @click="goDetail(item.id)">详情</a></td></tr></tbody></table></div>`,};//用户详情组件const UserInfo = {props: ["id"],template: `<div><h5>用户详情页 --- 用户Id为:{{id}}</h5><button @click="goback()">后退</button></div>`,methods: {goback() {// 实现后退功能this.$router.go(-1);},},};const Rights = {template: `<div><h3>权限管理区域</h3></div>`,};const Goods = {template: `<div><h3>商品管理区域</h3></div>`,};const Orders = {template: `<div><h3>订单管理区域</h3></div>`,};const Settings = {template: `<div><h3>系统设置区域</h3></div>`,};const Login = {data() {return {isLogin: window.isLogin,};},template: `<div><button @click="login" v-if="!isLogin">登录</button><button @click="logout" v-else>注销</button></div>`,methods: {login() {window.isLogin = true;this.$router.push(this.$route.query.redirect);},logout() {this.isLogin = window.isLogin = false;},},};// 创建路由对象const router = new VueRouter({routes: [{ path: "/login", component: Login },{path: "/",component: App,redirect: "/users",children: [{path: "/users",component: Users,meta: {auth: true,},},{ path: "/userinfo/:id", component: UserInfo, props: true },{ path: "/rights", component: Rights },{ path: "/goods", component: Goods },{ path: "/orders", component: Orders },{ path: "/settings", component: Settings },],},],});//实现全局守卫router.beforeEach((to, from, next) => {//to:去哪个页面,from来自哪个页面,next继续执行.//判断哪个路由需要进行守卫,这里可以通过元数据方式if (to.meta.auth) {if (window.isLogin) {next();} else {next("/login?redirect=" + to.fullPath);}} else {next();}});const vm = new Vue({el: "#app",router,});</script></body>
</html>

以上是全局守卫,对所有的路由都起作用。

但是,如果项目比较简单,路由规则定义的比较少,可以将守卫定位到某个路由规则内。这就是路由独享守卫

  // 创建路由对象const router = new VueRouter({routes: [{ path: "/login", component: Login },{path: "/",component: App,redirect: "/users",children: [{path: "/users",component: Users,meta: {auth: true,},beforeEnter(to, from, next) {if (window.isLogin) {next();} else {next("/login?redirect=" + to.fullPath);}},},{ path: "/userinfo/:id", component: UserInfo, props: true },{ path: "/rights", component: Rights },{ path: "/goods", component: Goods },{ path: "/orders", component: Orders },{ path: "/settings", component: Settings },],},],});

在上面的代码中,给/users这个路由守卫,注意这里的方法名为beforeEnter.同时,这里将守卫定义在/users路由规则内,所以不需要对元数据进行判断,只需要判断用户是否登录就可以了。(注意:在进行以上测试时,需要将全局守卫的代码注释掉)

组件内守卫

可以在路由组件内直接定义以下路由导航守卫。

beforeRouteEnter
beforeRouteUpdate
beforeRouteLeave

将如下的代码直接添加到组件内。

 const Users = {data() {return {userlist: [{ id: 1, name: "张三", age: 10 },{ id: 2, name: "李四", age: 20 },{ id: 3, name: "王五", age: 30 },{ id: 4, name: "赵六", age: 40 },],};},methods: {goDetail(id) {console.log(id);this.$router.push("/userinfo/" + id);},},template: `<div><h3>用户管理区域</h3><table><thead><tr><th>编号</th><th>姓名</th><th>年龄</th><th>操作</th></tr></thead><tbody><tr v-for="item in userlist" :key="item.id"><td>{{item.id}}</td><td>{{item.name}}</td><td>{{item.age}}</td><td><a href="javascript:;" @click="goDetail(item.id)">详情</a></td></tr></tbody></table></div>`,beforeRouteEnter(to, from, next) {if (window.isLogin) {next();} else {next("/login?redirect=" + to.fullPath);}},};

在上面的代码中,直接将路由守卫对应的方法添加到了组件中。

注意:在测试之前将路由规则中定义的路由守卫的代码注释掉。

这篇关于Vue-router:10、路由守卫的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/891574

相关文章

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob

Vuex Actions多参数传递的解决方案

《VuexActions多参数传递的解决方案》在Vuex中,actions的设计默认只支持单个参数传递,这有时会限制我们的使用场景,下面我将详细介绍几种处理多参数传递的解决方案,从基础到高级,... 目录一、对象封装法(推荐)二、参数解构法三、柯里化函数法四、Payload 工厂函数五、TypeScript

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo

CSS Padding 和 Margin 区别全解析

《CSSPadding和Margin区别全解析》CSS中的padding和margin是两个非常基础且重要的属性,它们用于控制元素周围的空白区域,本文将详细介绍padding和... 目录css Padding 和 Margin 全解析1. Padding: 内边距2. Margin: 外边距3. Padd