"导航" 表示路由正在发生改变vue
vue-router
提供的导航守卫主要用来经过跳转或取消的方式守卫导航。有多种机会植入路由导航过程当中,他们分为三种:git
注:参数或查询的改变并不会触发进入/离开的导航守卫 (你能够经过观察 $route
对象来应对这些变化,或使用 beforeRouteUpdate
的组件内守卫。)vue-router
给一张图直观的看一下:
浏览器
可使用 router.beforeEach
注册一个全局前置守卫bash
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
传递任意位置对象,且容许设置诸如 replace: true
、name: 'home'
之类的选项以及任何用在 router-link
的 to prop
或 router.push
中的选项。next(error)
: (2.4.0+) 若是传入 next
的参数是一个 Error 实例,则导航会被终止且该错误会被传递给 router.onError()
注册过的回调。确保要调用next
方法,不然钩子就不会被resolvedui
能够用router.beforeResolve
注册一个全局守卫。这和 router.beforeEach
相似,区别是在导航被确认以前,同时在全部组件内守卫和异步路由组件被解析以后,解析守卫就被调用。this
能够注册一个全局后置钩子,然而和守卫不一样的是,这些钩子不会接受next
函数也不会改变导航自己spa
router.afterEach((to, from) => {
// ...
})
复制代码
能够在路由配置上直接定义 beforeEnter
守卫:
const router = new VueRouter({
routes: [
{
path: '/foo',
component: Foo,
beforeEnter: (to, from, next) => {
// ...
}
}
]
})
复制代码
这些守卫与全局前置守卫的方法参数是同样的。
固然,也能够在路由组件内直接定义如下路由导航守卫:
beforeRouteEnter
beforeRouteUpdate
beforeRouteLeave
const Foo = {
template: `...`,
beforeRouteEnter (to, from, next) {
// 在渲染该组件的对应路由被 confirm 前调用
// 也就是进入新的组件时
// 不能获取组件实例 `this`
// 由于当守卫执行前,组件实例还没被建立
},
beforeRouteUpdate (to, from, next) {
// 在当前路由改变,可是该组件被复用时调用
// 举例来讲,对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候,
// 因为会渲染一样的 Foo 组件,所以组件实例会被复用。而这个钩子就会在这个状况下被调用。
// 能够访问组件实例 `this`
},
beforeRouteLeave (to, from, next) {
// 导航离开该组件的对应路由时调用
// 能够访问组件实例 `this`
}
}
复制代码
beforeRouteEnter
守卫 不能 访问 this
,由于守卫在导航确认前被调用,所以即将登场的新组件还没被建立。
不过,你能够经过传一个回调给 next
来访问组件实例。在导航被确认的时候执行回调,而且把组件实例做为回调方法的参数。
beforeRouteEnter (to, from, next) {
next(vm => {
// 经过 `vm` 访问组件实例
})
}
复制代码
注意 beforeRouteEnter
是支持给 next
传递回调的惟一守卫。对于 beforeRouteUpdate
和 beforeRouteLeave
来讲,this
已经可用了,因此不支持传递回调,由于没有必要了。
beforeRouteUpdate (to, from, next) {
// just use `this`
this.name = to.params.name
next()
}
复制代码
beforeRouteUpdate
是在路径发生改变时调用
这个离开守卫一般用来禁止用户在还未保存修改前忽然离开。该导航能够经过 next(false)
来取消。
beforeRouteLeave (to, from , next) {
const answer = window.confirm('Do you really want to leave? you have unsaved changes!')
if (answer) {
next()
} else {
next(false)
}
}
复制代码
1.导航被触发
2.在失活的组件里调用离开守卫。
3.调用全局的 beforeEach
守卫。
4.在重用的组件里调用 beforeRouteUpdate
守卫 (2.2+)。
5.在路由配置里调用 beforeEnter
。
6.解析异步路由组件。
7.在被激活的组件里调用 beforeRouteEnter
。
8.调用全局的 beforeResolve
守卫 (2.5+)。
9.导航被确认。
10.调用全局的 afterEach
钩子。
11.触发 DOM
更新。
12.用建立好的实例调用 beforeRouteEnter
守卫中传给 next
的回调函数。
//Router定义
Vue.use(Router)
const router = new Router({
...
})
//导航守卫
router.beforeEach((to, from, next) => {
console.log("导航前置守卫: beforeEach,");
next();
})
router.afterEach((to, from) => {
console.log("导航后置守卫: afterEach,");
})
router.beforeResolve ((to, from, next) => {
console.log("导航解析守卫: beforeResolve,");
next();
})
复制代码
组件钩子:
export default {
//钩子
beforeCreate(){
console.log("组件钩子: beforeCreate");
},
created(){
console.log("组件钩子: created");
},
beforeMount(){
console.log("组件钩子: beforeMount");
},
mounted(){
console.log("组件钩子: mounted");
},
beforeUpdate(){
console.log("组件钩子: beforeUpdate");
},
updated(){
console.log("组件钩子: updated");
},
beforeDestroy(){
console.log("组件钩子: beforeDestroy");
},
destoryed(){
console.log("组件钩子: destoryed");
},
beforeRouteEnter (to, from, next) {
console.log("组件内部守卫: beforeRouteEnter,");
next()
},
beforeRouteUpdate (to, from, next) {
console.log("组件内部守卫: beforeRouteUpdate,");
next()
},
beforeRouteLeave (to, from, next) {
console.log("组件内部守卫: beforeRouteLeave,");
next()
}
}
复制代码
执行输出顺序:
导航前置守卫: beforeEach
组件内部守卫: beforeRouteEnter
导航解析守卫: beforeResolve
导航后置守卫: afterEach
组件钩子: beforeCreate
组件钩子: created
组件钩子: beforeMount
组件钩子: mounted
//执行跳转
组件内部守卫: beforeRouteLeave
导航前置守卫: beforeEach
导航解析守卫: beforeResolve
导航后置守卫: afterEach
组件钩子: beforeDestroy
复制代码
关于 Vue组件 的生命周期与钩子函数,请参考个人文章《Vue 生命周期与钩子函数》