vue2.0 实现导航守卫(路由守卫)---登录验证

路由跳转前做一些验证,比如登录验证,是网站中的普遍需求。

对此,vue-route 提供的 beforeRouteUpdate 可以方便地实现导航守卫(navigation-guards)。

导航守卫(navigation-guards)这个名字,听起来怪怪的,但既然官方文档是这样翻译的,就姑且这么叫吧。

贴上文档地址:https://router.vuejs.org/zh-cn/advanced/navigation-guards.html

全局守卫

你可以使用 router.beforeEach 注册一个全局前置守卫:

const router = new VueRouter({ ... })

router.beforeEach((to, from, next) => {
  // ...
})

当一个导航触发时,全局前置守卫按照创建顺序调用。守卫是异步解析执行,此时导航在所有守卫 resolve 完之前一直处于 等待中。

每个守卫方法接收三个参数:

  • to: Route: 即将要进入的目标 路由对象
  • from: Route: 当前导航正要离开的路由
  • next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。
    • next(): 进行管道中的下一个钩子。如果全部钩子执行完了,则导航的状态就是 confirmed (确认的)。
    • next(false): 中断当前的导航。如果浏览器的 URL 改变了(可能是用户手动或者浏览器后退按钮),那么 URL 地址会重置到 from 路由对应的地址。
    • next(‘/‘) 或者 next({ path: ‘/‘ }): 跳转到一个不同的地址。当前的导航被中断,然后进行一个新的导航。
    • next(error): (2.4.0+) 如果传入 next 的参数是一个 Error 实例,则导航会被终止且该错误会被传递给 router.onError() 注册过的回调。

确保要调用 next 方法,否则钩子就不会被 resolved。

 

下面写一个例子:

  1. 列举需要判断登录状态的“路由集合”,当跳转至集合中的路由时,如果“未登录状态”,则跳转到登录页面LoginPage;
  2. 当直接进入登录页面LoginPage时,如果“已登录状态”,则跳转到首页HomePage;

import Vue from ‘vue‘;
import Router from ‘vue-router‘;
import LoginPage from ‘@/pages/login‘;
import HomePage from ‘@/pages/home‘;
import GoodsListPage from ‘@/pages/good-list‘;
import GoodsDetailPage from ‘@/pages/good-detail‘;
import CartPage from ‘@/pages/cart‘;
import ProfilePage from ‘@/pages/profile‘;

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: ‘/‘,  // 默认进入路由
      redirect: ‘/home‘   //重定向
    },
    {
      path: ‘/login‘,
      name: ‘login‘,
      component: LoginPage
    },
    {
      path: ‘/home‘,
      name: ‘home‘,
      component: HomePage
    },
    {
      path: ‘/good-list‘,
      name: ‘good-list‘,
      component: GoodsListPage
    },
    {
      path: ‘/good-detail‘,
      name: ‘good-detail‘,
      component: GoodsDetailPage
    },
    {
      path: ‘/cart‘,
      name: ‘cart‘,
      component: CartPage
    },
    {
      path: ‘/profile‘,
      name: ‘profile‘,
      component: ProfilePage
    },
    {
      path: ‘**‘,   // 错误路由
      redirect: ‘/home‘   //重定向
    },
  ]
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  console.log(‘navigation-guards‘);
  // to: Route: 即将要进入的目标 路由对象
  // from: Route: 当前导航正要离开的路由
  // next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。

  const nextRoute = [‘home‘, ‘good-list‘, ‘good-detail‘, ‘cart‘, ‘profile‘];
  let isLogin = global.isLogin;  // 是否登录
  // 未登录状态;当路由到nextRoute指定页时,跳转至login
  if (nextRoute.indexOf(to.name) >= 0) {
    if (!isLogin) {
      console.log(‘what fuck‘);
      router.push({ name: ‘login‘ })
    }
  }
  // 已登录状态;当路由到login时,跳转至home
  if (to.name === ‘login‘) {
    if (isLogin) {
      router.push({ name: ‘home‘ });
    }
  }
  next();
});

export default router;

.

原文地址:https://www.cnblogs.com/jianxian/p/11966270.html

时间: 2024-08-30 11:23:03

vue2.0 实现导航守卫(路由守卫)---登录验证的相关文章

路由实现登录验证

路由验证用 router.beforeEach( (to, from, next) => { } meta:{requireAuth:true}, 使用meta和路由守卫设置登录权限 原文地址:https://www.cnblogs.com/zbx-boke/p/9651755.html

Vue2.0 Router 导航守卫

官方文档地址:https://router.vuejs.org/zh-cn/advanced/navigation-guards.html 全局守卫 可以使用 router.beforeEach 注册一个全局前置守卫: 例如: 判断在用户未登录时, 跳转到 "/login" 的路由,  登录时,直接跳转到默认路由 const router = new VueRouter({ ... }) // 实例化 router router.beforeEach((to, from, next)

vue2.0 页面A跳转到页面B,B页面停留在A页面的滚动位置的解决方法

如果页面A沿Y轴滚动一段距离,然后跳转到页面B: 在进入B页面时,B页面已经滚到页面A的距离,返回页面A,发现A还在之前的滚动位置: 这样体验就很不好,所以我们要进行一些处理: 我的方法是:在路由守卫回调中,设置每次进入路由时,将window的scroll值设置为0:window.scroll(0, 0);代码如下 // 全局路由守卫 router.beforeEach((to, from, next) => { // to: Route: 即将要进入的目标 路由对象 // from: Rout

Vue中的导航守卫(路由守卫)

当做Vue-cli项目的时候感觉在路由跳转前做一些验证,比如登录验证,是网站中的普遍需求. 对此,vue-router 提供的 beforeEach可以方便地实现全局导航守卫(navigation-guards).组件内部的导航守卫函数使用相同,只是函数名称不同(beforeRouteEnter .beforeRouteUpdate(2.2 新增) .beforeRouteLeave). 官方文档地址:https://router.vuejs.org/zh-cn/advanced/navigat

angular4.0 路由守卫详解

在企业应用中权限.复杂页多路由数据处理.进入与离开路由数据处理这些是非常常见的需求. 当希望用户离开一个正常编辑页时,要中断并提醒用户是否真的要离开时,如果在Angular中应该怎么做呢? 其实Angular路由守卫属性可以帮我们做更多有意义的事,而且非常简单. Angular 的 Route 路由参数中除了熟悉的 path.component 外,还包括四种是否允许路由激活与离开的属性. canActivate: 控制是否允许进入路由. canActivateChild: 等同 canActi

Angular4.x通过路由守卫进行路由重定向,实现根据条件跳转到相应的页面

需求: 最近在做一个网上商城的项目,技术用的是Angular4.x.有一个很常见的需求是:用户在点击"我的"按钮时读取cookie,如果有数据,则跳转到个人信息页面,否则跳转到注册或登录页面 解决 在这里通过Angular的路由守卫来实现该功能. 1. 配置路由信息 const routes = [ { path: 'home', component: HomeComponent }, { path: 'product', component: ProductComponent },

Angular4 路由守卫

序言 在不设置路由守卫的时候,任何用户能在任何时候导航到任何地方的,但是在某些场景为了安全,我们需要登录的用户才能导航到对应的页面.这时候我们需要给组件添加路由守卫. 步骤 1.分层路由介绍 CanActivate:处理导航到某路由的情况 CanActivateChild:处理导航到某子路由的情况 CanDeactivate:处理从当前路由离开的情况 Resolve:在路由激活之前获得路由数据 CanLoad:处理异步导航到某特性模块的情况 2.声明 这里的路由守卫使用与Angular-cli使

vue的生命周期和路由守卫

组件相关钩子函数: beforeCreate.created.beforeMount.mounted.beforeUpdate.updated.beforeDestroy.destoryed 还有两个特殊的(使用keep-alive):activated.deactivated(不详述) v2.5.0+新增: errorCaptured (暂时还不知道咋用) 路由守卫: 全局&路由独享:beforeEach.beforeResolve(v2.5.0+新增).afterEach :beforeEn

Vue router 全局路由守卫

记录一下全局路由守卫的使用: 方法一:定义一个数组用于检测与管理需要登录的页面,全局路由守卫配合本地存储判断是否跳转 import Vue from 'vue' import Router from 'vue-router' import store from './../store' import Home from 'components/home/home' // 主页组件 // 其它组件... import Cart from 'components/cart/cart' // 购物车组