前言:本文是使用导航守卫和路由元信息编写的demo。目的是拦截和查询。本文涉及的知识在之前和之前的文章中都有详细说明。如果你对某些知识不是很熟悉,可以借鉴前面的章节,或者给我留言。源码地址:https://github.com/coder-Wang...克隆时切换到分支6.导航守卫拦截,具体操作:gitclone-b6.导航守卫拦截https://github.com/coder-王宇/vue-test-code.git1。先看效果1.1访问控制1.2登录1.3访问需要权限页面1.4留下查询2.源码解读其实很简单,这里说几个重点。router.js:路由配置:{path:'/about',component:()=>import("../components/about.vue"),meta:{requestFlag:false,leaveFlag:true,},},{path:'/hy',组件:()=>import("../components/hy.vue"),redirect:'/hy/jd',children:[{path:'jd',组件:()=>import("../components/hy/jd.vue"),meta:{requestFlag:true,},},],meta:{requestFlag:true,},},{path:'/login',组件:()=>import("../components/login.vue")},全局守卫:router.beforeEach((to,from,next)=>{lethaveFlag=to.matched.some(item=>item.meta.requestFlag)if(haveFlag){document.cookie.includes('=')?next():window.confirm("您需要登录才能浏览,您登录了吗?")?next('/login'):next(false)}else{next()}})login.vue:
