vue中的前置守卫
前置守卫是为了验证用户信息真实性,一些内容只能在用户登陆以后才能进行查看,例如个人中心,我的购物车,等个人页面,非隐私页面
用router.beforeEach进行验证,这个方法必须写在router实例之后
三个参数 to===到哪里去
from===从哪里来
next===放行
router.beforeEach((to,from,next)=>{
        if(to.matched.some((route)=>route.meta.requireAuth)){
            if(localStorage.getItem('ticket')){
                next()
            }else{
                next('/login?returnURL='+to.path)
            }
        }else{
            next()
        }
    });
to.matched.some((route)=>route.meta.requireAuth)是有效的检验方法,可以从父元素以及祖先元素进行验证,
在你需要守卫的组件上加入meta标签就可以啦,
meta:{
        requireAuth:true
     }
在vue cli脚手架中需吧router中代码稍加改动就可以使用啦
const router= new Router({
  linkExactActiveClass: 'active',
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path:'/',
      component:Index,
      children:[
        {
          path:'channel/:id',
          component:Channel,
          children:[
            {
              path:'/topic/:num',
              component:Topic
            }
          ]
        }
      ]
    },{
      path:'/login',
      component:Login,
      children:[
        {
          path:'reg',
          component:Reg,
          meta:{
            requireAuth:true
          },
        },
        {
          path:'myself',
          component:Myself,
          meta:{
            requireAuth:true
          },
        }
      ]
    }
  ]
})
router.beforeEach((to,from,next)=>{
  if(to.matched.some(route=>route.meta.requireAuth)){
    const url=localStorage.getItem('login');
    if(url=='1'){
      next();
    }else{
      next('/login?returnURL='+to.path);
    }
  }else{
    next();
  }
});
export default router
把 export 放在最后,这样就可以实现前置守卫
vue中的前置守卫的更多相关文章
- vue中的导航守卫
		官方文档地址: 导航守卫:https://router.vuejs.org/zh-cn/advanced/navigation-guards.html 好的,重点内容 router.beforeEac ... 
- Vue中的导航守卫(路由守卫)
		当做Vue-cli项目的时候感觉在路由跳转前做一些验证,比如登录验证,是网站中的普遍需求. 对此,vue-router 提供的 beforeEach可以方便地实现全局导航守卫(navigation-g ... 
- 5分钟学会vue中的路由守卫(导航守卫)
		在项目开发中每一次路由的切换或者页面的刷新都需要判断用户是否已经登录,前端可以判断,后端也会进行判断的,我们前端最好也进行判断. vue-router提供了导航钩子:全局前置导航钩子 beforeEa ... 
- nuxtjs中使用路由守卫
		在vue中的路由守卫一般是来验证token失效什么的,当然也可以设置权限啦,在nuxtjs中如何使用路由守卫呢,话不多说,直接上代码 在plugins目录下简历route.js export defa ... 
- vue中的路由独享守卫的理解
		1.vue中路由独享守卫意思就是对这个路由有一个单独的守卫,因为他的守卫方式于其他的凡是不太同 独享守卫于前置守卫使用方法大致是一样的 在路由配置的时候进行配置, { path:'/login', c ... 
- 解决vue组件内前置路由守卫beforeRouteEnter无法获取上下文this
		问题描述 vue框架,只有在报名页面报名成功,然后自动跳转到订单详情,才弹出一个引流弹窗,其他情况均不弹出,我就想到使用vue 的组件内前置守卫beforeRouteEnter来实现.beforeRo ... 
- vue之路由导航守卫-全局前置守卫
		一.使用方式 全局前置守卫用于在路由配置生效之前进行一些动作,可以使用 router.beforeEach 注册一个全局前置守卫: const router = new VueRouter({ ... ... 
- vue中keepAlive的使用
		在开发中经常有从列表跳到详情页,然后返回详情页的时候需要缓存列表页的状态(比如滚动位置信息),这个时候就需要保存状态,要缓存状态:vue里提供了keep-alive组件用来缓存状态.可以用以下几种方案 ... 
- vue中使用keepAlive组件缓存遇到的坑
		项目开发中在用户由分类页category进入detail需保存用户状态,查阅了Vue官网后,发现vue2.0提供了一个keep-alive组件. 上一篇讲了keep-alive的基本用法,现在说说遇到 ... 
随机推荐
- nginx配置websocket
			有时候我们需要给websocket服务端做一下nginx的配置,比如需要给websocket服务端做负载均衡,或者,有些系统要求访问websocket的时候不能带端口,这时候我们就需要用nginx来进 ... 
- [leetcode]528. Random Pick with Weight按权重挑选索引
			Given an array w of positive integers, where w[i] describes the weight of index i, write a function ... 
- [leetcode]114. Flatten Binary Tree to Linked List将二叉树展成一个链表
			Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ... 
- java  线程Thread 技术--1.5 Executor Executors,ThreadPool,Queue
			Executors : Executors ,就是一个线程工具类:大部分操作线程的方法,都可以在这个工具类中就行创建,执行,调用一些线程的方法: Executor : 用于执行和提交一个runnabl ... 
- 利用scp 远程上传下载文件/文件夹
			scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port ... 
- 关于AndroidStudio的打包数字签名以及多渠道发布
			AndroidStudio右侧Gradle里边的build(工程下的)是可以生成未签名的debug和release的apk包生成前可以先clean一下工程 app下的build.gradle里边的 l ... 
- DialogActivity
			<?xml version="1.0" encoding="utf-8"?> <resources> <style name=&q ... 
- ES6  Decorator 修饰器
			目的: 修改类的一种方法,修饰器是一个函数 编译: 安装 babel-plugin-transform-decortators-legacy .babelrd plugins: [&quo ... 
- vue总结2
			1. 给router-link添加事件 之前用v-link 现在用 router-link 添加事件要用原生的.native修饰v-on <my-component v-on:click.nat ... 
- AttributeError: 'WebDriver' object has no attribute 'switchTo'
			不在错误中爆发,就在错误中死亡呀. from selenium import webdriver from selenium.webdriver.support.ui import WebDriver ... 
