reactrouter4路由钩子_react router @4 和 vue路由 详解(八)vue路由守卫

13、vue路由守卫

a、beforeEach

全局守卫

(每个路由调用前都会触发,根据from和to来判断是哪个路由触发)

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

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

// ...

})

//每个守卫功能都有三个参数:

//to: Route:导航到的目标Route对象

//from: Route:当前路线被导航离开

//next: Function:必须调用此函数来解析钩子

// next():继续前进到管道中的下一个钩子。如果没有留下挂钩,则确认导航。

// next(false):中止当前导航。如果浏览器URL已更改(由用户手动或通过后退按钮),则会将其重置为from路径的URL 。

// next('/')或next({ path: '/' }):重定向到其他位置。当前导航将中止,并将启动一个新导航。你可以通过任何位置对象next,它允许您指定类似的选项replace: true,name: 'home'在使用任何选项router-link的to道具或router.push

// next(error):(2.4.0+)如果传递给的参数next是一个实例Error,导航将被中止,错误将传递给通过注册的回调router.onError()。

`

举个🌰

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;

b、beforeResolve(2.5.0新增)

全局解析守卫

和router.beforeEach类似,区别是在导航被确认之前,同时在所有组件内守卫和异步路由组件被解析之后,解析守卫就被调用

c、afterEach

全局后置钩子

router.afterEach((to, from) => {

// ...

})

//不会接受next函数也

//也不会改变导航本身

d、beforeEnter

路由独享的守卫

const router = new VueRouter({

routes: [

{

path: '/foo',

component: Foo,

beforeEnter: (to, from, next) => {

// ...

}

}

]

})

//与全局前置守卫的方法参数是一样的

e、

组件内的守卫

beforeRouteEnter

beforeRouteUpdate (2.2新增)

beforeRouteLeave

const Foo = {

template: `...`,

beforeRouteEnter (to, from, next) {

// 在渲染该组件的对应路由被 confirm 前调用

// 不!能!获取组件实例 `this`

// 因为当守卫执行前,组件实例还没被创建

},

beforeRouteUpdate (to, from, next) {

// 在当前路由改变,但是该组件被复用时调用

// 举例来说,对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候,

// 由于会渲染同样的 Foo 组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。

// 可以访问组件实例 `this`

},

//离开守卫通常用来禁止用户在还未保存修改前突然离开。导航该可以通过next(false)来取消

beforeRouteLeave (to, from, next) {

// 导航离开该组件的对应路由时调用

// 可以访问组件实例 `this`

}

}

//beforeRouteEnter守卫不能访问this,

//因为守卫在导航确认前被调用,因此即将登场的新组件还没被创建。

//不过,可以你传通过一个回调给next来访问组件实例。

//在导航被确认的时候执行回调,并且把组件实例作为回调方法的参数。

beforeRouteEnter (to, from, next) {

next(vm => {

// 通过 `vm` 访问组件实例

})

}

完整的导航解析流程

导航被触发。

在失活的组件里调用离开守卫。

全局调用的beforeEach守卫。

在重用的组件里调用beforeRouteUpdate守卫(2.2+)。

在路由配置里调用beforeEnter。

解析异步路由组件。

在被激活的组件里调用beforeRouteEnter。

调用全局的beforeResolve守卫(2.5+)。

导航被确认。

全局调用的afterEach钩子。

触发DOM更新。

创建³³用实例好的调用beforeRouteEnter守卫中传给next的回调函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值