Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create Vue route with empty component?

Say I have these routes:

/admin/login
/admin
/admin/dashboard

The purpose of /admin is there to redirect users to either /admin/login or /admin/dashboard based on their authenticated state. So it has no template/component. What's the correct way to achieve this?

Should I create a Admin.vue component only to redirect users? Or is there another method of doing this?

like image 484
Joshua Leung Avatar asked Dec 03 '25 14:12

Joshua Leung


1 Answers

Sounds like you want a navigation guard. See https://router.vuejs.org/guide/advanced/navigation-guards.html

For example

[{
  path: '/admin',
  beforeEnter (to, from, next) {
    if (userIsAuthenticated) {
      next({ name: 'admin-dashboard' }) // or next('/admin/dashboard')
    } else {
      next({ name: 'admin-login' })     // or next('/admin/login')
    }
  }
}, {
  path: '/admin/dashboard',
  name: 'admin-dashboard',
  component: ...
}, {
  path: '/admin/login',
  name: 'admin-login',
  component: ...
}]

So no, you don't need an Admin component for a route that never renders anything.


You'll probably also want a global navigation guard to protect pages that require authentication.

like image 96
Phil Avatar answered Dec 06 '25 13:12

Phil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!