Angular2+ | How to define routing with params in NgModule

Jam Nguyen

I'm quite new with Angular routing and couldn't find any solution for this case. I've got a Login component and a User module. There's one <router-outlet> in App component, one in UserBase component in User module. Here's my routing declaration.

app.module.ts

const routes: Routes = [
  {
    path: 'login',
    component: LoginComponent
  },
];

imports: [
    RouterModule.forRoot(routes),
    UserModule,
],

user.module.ts

const routes: Routes = [
  {
    path: ':username',
    component: UserBaseComponent,
    children: [
      {
        path: '',
        component: UserDetailsComponent,
      },
      {
        path: 'progress',
        component: ProgressComponent,
      },
      {
        path: 'timeline',
        component: TimelineComponent,
      },
      {
        path: 'leaderboard',
        component: LeaderboardComponent,
      }
    ],
  }
];

imports: [
    RouterModule.forChild(routes)
]

It works fine with the path for user module, but it keeps recognizing 'login' as an username and not navigate to Login component. Actually, if I add a static path before the param like this, it works correctly.

{
  path: 'user',
  children: [
    {
      path: ':username',
      component: UserBaseComponent,
      children: [
        {
          path: '',
          component: UserDetailsComponent,
        },
        {
          path: 'progress',
          component: ProgressComponent,
        },
        {
          path: 'timeline',
          component: TimelineComponent,
        },
        {
          path: 'leaderboard',
          component: LeaderboardComponent,
        }
      ],
    }
  ]
}

Could anyone explain why?

Ruben Vermeulen

When you import modules with their own routing inside your main module the routes for the sub modules are placed in front of the main routes.

So you're routing looks like this when you import the UserModule.

- :username
    - /
    - progress

- login

When the routing is read it starts at the top, so :username is read first. That route matches everything, so no other route will be accessed.

You can try and lazy load your user module, this way you are in control of the order of routes. Notice that you don't declare the UserModule as an import anymore.

const routes: Routes = [
  {
    path: 'login',
    component: LoginComponent
  },
  {
    path: '',
    loadChildren: '<PATH_TO_MODULE>/user.module#UserModule'
  },
];

imports: [
    RouterModule.forRoot(routes)
]

Here is a Stackblitz: https://stackblitz.com/edit/ng-stackoverflow-52391592

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to Unit Test Angular 2 routing params

How to use service in angular2 NgModule

Angular 2 Routing - ModuleWithProviders v NgModule

Angular 2: how to get params from URL without using Routing?

How to define hierarchy routing in angular modules

Angular How to cancel http call on routing params change

How to use Angular2 routing

Angular2+ routing - optional route params

Routing issue in angular while define routing with parameter

Angular2 how to Mock Activated Route Params subscribed

Angular2, how to clear URL query params on router navigate

spring and angular2 how to request the post data with params?

How to correctly import FormGroup in NgModule in Angular 2

Angular routing canActivate AuthGuard transmit query params

Nested ionic-angular tabs routing with params

How to pass query params to load angular component that is loaded based on angular routing

How to define a literal type base on input params

How to define a page with multiple search params in SvelteKit?

How to define child routes for specific params?

How can define type of {state,...params}

How to set application context path in angular2 routing properly?

How to detect routing to same page in Angular2?

How to save Data During Routing in angular2

How to include a param in browsers url when using params instead of url in angular ui routing?

Angular define route params from root

Web Api 2 Attribute Routing and Angular JS params

Angular2 How to Define Input Property in Plain JS

How to use variable to define templateUrl in Angular2

How to define a nested object within model? (angular2/typescript)