首页 > 其他分享 >vue路由守卫用于登录验证权限拦截

vue路由守卫用于登录验证权限拦截

时间:2022-09-29 23:45:58浏览次数:69  
标签:vue name next import login 权限 路由

vue路由守卫用于登录验证权限拦截

vue路由守卫 - 全局(router.beforeEach((to, from, next) =>来判断登录和路由跳转状态)

主要方法

  • to:进入到哪个路由去
  • from:从哪个路由离开
  • next:路由的控制参数,常用的有next(true)和next(false)

首先判断进入的是否是login页面?然后再判断是否已经登陆?
已经登陆了就进入你要跳转的页面,没登录就进入login页面

router路由设置

index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'



Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
   VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    name: 'login',
    component: ()=>import('../views/Login/LoginView.vue')
  },
  {
    path: '/register',
    name: '注册',
    component: ()=>import('../views/Register/RegisterView.vue')
  },
  {
    path: '/index',
    name: 'index',
    component: ()=>import('../views/Index/Index.vue'),
    // redirect:'/manage',
    children:[
      {
        path: '/manage',
        name: '图书管理',
        component:  ()=>import('../views/Manage/Manage.vue')
      },
    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = new VueRouter({
  routes
});

router.beforeEach((to, from, next) => {
  const isLogin = window.localStorage.getItem('main'); //获取本地存储的登陆信息
  console.log(isLogin)
  console.log("to:"+to.name) //进入到哪个路由去
  console.log("from:"+from) //从哪个路由离开
  //next:路由的控制参数,常用的有next(true)和next(false)
  //next() 直接进to 所指路由
  //next('route') 跳转指定路由
  if (to.name == 'login') { //判断是否进入的login页
    if (isLogin) {  //判断是否登陆
      next({ name: 'index' });  //已登录,跳转首页
    } else {
      next();  //没登录,继续进入login页
    }
  } else { //如果进入的非login页
    if (isLogin) {   //同样判断是否登陆
      next();  //已登录,正常进入当前页面
    } else {
      next({ name: 'login'});  //没登录,跳转到login页
    }
  }
});


export default router

标签:vue,name,next,import,login,权限,路由
From: https://www.cnblogs.com/b10100912/p/16743509.html

相关文章

  • RabbitMQ 入门系列:3、基础含义:持久化、排它性、自动删除、强制性、路由键。
    系列目录​​RabbitMQ入门系列:1、MQ的应用场景的选择与RabbitMQ安装。​​​​RabbitMQ入门系列:2、基础含义:链接、通道、队列、交换机。​​​​RabbitMQ入门系列:3、基础......
  • 秋式开源团队:权限管理系统需求与分析
    以下为正文内容:项目的目标提供一个调用简单、可复用性高、满足一般需求的权限管理模块。为需要对权限管理的系统节省开发本。产品的用户开发基于.net且权限管理较为复杂的系......
  • vue实现Popup弹窗
     1.首先我们新建一个Popup.vue,这个组件有一个关闭按钮。这个组件十子组件传值个父组件,通过$emit发送值给父组件。     2.父组件导入使用组件,定义点击事件......
  • 缺省路由(默认路由)
    缺省路由(默认路由)换回接口:interfaceloopback0ipaddress215.17.1.34255.255.255.255   常用查看配置命令:displaycurrent-configuration--查看所有......
  • 路由优先级和路由度量
     路由优先级和路由度量内部优先级:不能修改;外部优先级:可以修改;平时所描述的优先级都指外部优先级。默认情况下,对于直连静态 rip ospf,内部优先级和外部优先级一样,路......
  • oracle查询某用户授予出去以及被授予的对象权限
    文档课题:oracle查询某用户授予出去以及被授予的对象权限.>showuserUseris"LEO">createtabletestasselect*fromall_objects;Tablecreated.>selectcount(*)fr......
  • Oracle数据库用户权限分析
    文档课题:Oracle数据库用户权限分析.1、查询权限普通用户查询自己所拥有的所有权限.>showuserUSERis"LEO">select*fromsession_privs;PRIVILEGE--------------------......
  • vue 动态组件component :is
    示例<componentv-bind:is="currentComponent"></component>currentComponent是要展示的组件,根据具体代码逻辑,currentComponent赋值为不同的组件在切换时保持组件状态,......
  • vue route路由使用
    <router-linkto="/home">home</router-link><router-link:to="{name:'user',params:{userId:'123'}}">user</router-link><router-link:to="{path:'/user',......
  • vue computed传参
    通过不同的数据,计算属性返回不同的结果,就需要给计算属性传参,通过返回一个带参数的箭头函数,可以实现计算属性传参computed:{//计算属性computedData(){//......