Trung BìnhVue.js iconVue.js

Navigation guards là gì? beforeEach, beforeEnter, beforeRouteEnter?

Navigation guards cho phép control navigation — xác thực, redirect, cancel.

javascript
// Global guard
router.beforeEach((to, from) => {
  if (to.meta.requiresAuth && !isLoggedIn()) {
    return { name: 'Login' }  // redirect
  }
})

// Per-route guard
{ path: '/admin', component: Admin, beforeEnter: (to, from) => { ... } }

In-component guards (Options API): beforeRouteEnter, beforeRouteUpdate, beforeRouteLeave.

  • Trong Composition API dùng onBeforeRouteLeave, onBeforeRouteUpdate.
  • Return false để cancel, return route location để redirect, return undefined/true để proceed.

Xem toàn bộ Vue.js cùng filter theo level & chủ đề con.

Mở danh sách Vue.js