Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue-Router Abstract Parent Routes

I am trying to migrate my current site to vuejs. The site map must be:

/login
/signup
/password-reset
/browse
/search
... dozens of other routes

As some of these routes share a lot of fx, I've made them the children of parent routes:

[{ // public routes
  path: '/', 
  component: Auth,
  children: [
    { path: '/login', component: Login },
    { path: '/signup', component: Signup },
    { path: '/password-reset', component: PasswordReset },
  ]
}, 
{ // routes behind Authentication
  path: '/', 
  component: Home,
  children: [
    { path: '/browse', component: Browse },
    { path: '/search', component: Search }
  ]
}]

The problem is obvious: The Auth and Home base components now are technically the same route path and I get routing errors. Since I will have a lot of routes sharing the same base component and fx, I'd like to have them be children of these abstract states.

Question 1: How can I implement these routes and their parent abstract states without conflict and without having to add all the wrapping logic to the children?

Question 2: How can I make it so the parent states () are not routeable or if they are, the default to a child state?

like image 223
jbodily Avatar asked Jun 20 '17 22:06

jbodily


1 Answers

Whenever more than one route shares the same path, the first route in the array takes priority. So you need to put the Home route before the Auth route. That way, the / path will always match the Home route and not the Auth route. That also means that it is impossible to route directly to the Auth route, which is what you want.

If you do not want the Home route to be accessible, you can specify a redirect that should occur when it is matched exactly:

{
  path: '/',
  component: Home,
  redirect: '/browse',           // Redirect to path, or
  redirect: { name: 'browse' },  // Redirect to named route
  children: ...
}

If your Auth component has no auth-specific UI, you mightn't want to use "abstract" routes like this to enforce the authentication. There's plenty of information about how to achieve this in vue-router; here's one way:

// Routes

{
  path: '/profile',
  component: Profile,
  meta: { auth: true },
}

// Router hooks

router.beforeEach((to, from, next) => {
  if (to.matched.some(route => route.meta.auth) && !authenticated) {
    next('/login');
  } else {
    next();
  }
});
like image 132
Decade Moon Avatar answered Nov 11 '22 11:11

Decade Moon