vue-router защита навигации вы можете определить глобальные перехватчики для вызова beforeEach изменения маршрутизатора или afterEach .
например, пример для защиты перед маршрутом :
//router.js
const router = new VueRouter({
routes: [
{
path: '/night',
component: nightComponent,
beforeEnter: (to, from, next) => {
const currentHour = new Date().getHours();
if(currentHour < 6){
next(); // this function will trigger the routing. in my example, i
//call it only if its not morning yet.
}
}
}
]
})
Вы также можете определить внутрикомпонентную защиту , дляпринять меры по изменению этого конкретного маршрута.
new Vue({
el: "#app",
router,
data: {},
methods: {},
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.
if(confirm('are you sure you want to leave this page?')){
next();
}
}
}
})