0

I have a main-routing module in which I redirect all the different routes in my app.

const routes: Routes = [
  {
    path: '',
    component: MainComponent,
    children: [
      { path: '', pathMatch: 'full', redirectTo: '' }, // The Problem
{ path: 'startComponent1', loadChildren: loadStartComponent1Module(), data: { title: 'startComponent1' } },
{ path: 'startComponent2', canDeactivate: [ExternGuard], loadChildren: loadStartComponent2Module(), data: { title: 'startComponent2' } },
    ]
  }
]

The loadStartComponent1Module() functions only pass the lazyloading string.

So far so good. Now I have two different types of users. Usertype 1:

  • Can access startComponent1
  • Can access startComponent2
  • Routing starts at Component1

I would change my routing to this:

//... unchanged
    { path: '', pathMatch: 'full', redirectTo: 'startComponent1' }, // The Problem
//... unchanged

My second User:

  • Cannot access startComponent1
  • Can access startComponent2
  • Routing starts at Component2

The access I can control over guards. But what can I do with the empty path? Can I distinguish beetween user 1 and 2 ? Something like this:

//will not work
//...unchanged
    { path: '', pathMatch: 'full',

    if(userA){
    redirectTo: 'startComponent1'
    }else {
    redirectTo: 'startComponent2'
    }
      }, // The Problem
//...unchanged

Should I attempt to solve this problem with resolvers? Should I define 2 different arrays for both types of users where only the problem line is changed?

But maybe there is an easier solution for this problem.

lynxSven
  • 555
  • 1
  • 10
  • 27
  • You should add routing logic to your `MainComponent` : redirect to where you want, and the default route should redirect to the main component. –  Jun 15 '18 at 08:05
  • Possible duplicate of [Angular2 conditional routing](https://stackoverflow.com/questions/34660263/angular2-conditional-routing) – Yassine Ben Hamida Jun 15 '18 at 08:26
  • Wouldn't be a duplicate. A guard primarily checks for authorization for a particular module. If user has permission for a particular route than ok, else redirect to login... You can redirect to another app based route but that's a deviation from the purpose... Just my thoughts – NitinSingh Jun 16 '18 at 12:37

0 Answers0