website/themes/cactus/exampleSite/content/posts/2019-01-19-vue-navigation-guards.md
Navigation guards are provided by vue-router.
Three ways to hook:
NOTE:
$route object to react to those changes, or use the beforeRouteUpdate in-component guard.const router = new VueRouter({ ... })
// Before Guards
router.beforeEach((to, from, next) => {
// ...
})
// Resolve Guards
// beforeResolve guards will be called right before the navigation is confirmed
// after all in-component guards and async route components are resolved
router.beforeResolve((to, from, next) => {
// ...
})
// After Hooks
router.afterEach((to, from) => {
// ...
})
const router = new VueRouter({
routes: [
{
path: '/foo',
component: Foo,
beforeEnter: (to, from, next) => {
// ...
}
}
]
})
const Foo = {
template: `...`,
beforeRouteEnter (to, from, next) {
// called before the route that renders this component is confirmed.
// does NOT have access to `this` component instance,
// because it has not been created yet when this guard is called!
// However, you can access the instance by passing a callback to next.
// The callback will be called when the navigation is confirmed
// and the component instance will be passed to the callback as the argument
beforeRouteEnter (to, from, next) {
next(vm => {
// access to component instance via `vm`
})
}
},
beforeRouteUpdate (to, from, next) {
// called when the route that renders this component has changed,
// but this component is reused in the new route.
// For example, for a route with dynamic params `/foo/:id`, when we
// navigate between `/foo/1` and `/foo/2`, the same `Foo` component instance
// will be reused, and this hook will be called when that happens.
// has access to `this` component instance.
},
beforeRouteLeave (to, from, next) {
// called when the route that renders this component is about to
// be navigated away from.
// has access to `this` component instance.
}
}