1

I have following app-routing.module.ts:

  {
    path: 'discover',
    loadChildren: () => import('./components/platform/user-profile/platform.module').then(m => m.PlatformModule)
  },
  {
    path: ':userRoute',
    loadChildren: () => import('./components/platform/user-profile/platform.module').then(m => m.PlatformModule)
  },

My goal is that /discover should open DiscoverPageComponent from PlatformModule

/userName1 should open UserPageComponent from PlatformModule

My platform-routing.module.ts contains following:

  {
    path: '',
    component: UserProfileComponent,
  },
  {
    path: 'discover',
    component: DiscoverPageComponent,
  },

This doesn't work as /discover will always open the UserProfileComponent instead of the DiscoverPageComponent. I can only open the DiscoverPageComponent from /userName1/discover

How can I have those two different routes open their specific component from the same lazy loaded module?

Stackblitz: https://stackblitz.com/edit/angular-w3rc5g Please see /discover and /anyUserName1

6
  • 1
    Can you reproduce it on stackblitz? Commented Apr 27, 2020 at 13:52
  • partial fix - in platform-routing.module.ts replace UserProfileComponent with DiscoverPageComponent and DiscoverPageComponent with UserProfileComponent . now /discover with open DiscoverPageComponent Commented Apr 27, 2020 at 13:55
  • I would suggest u use something like a platform-container.component.ts with a router-outlet. then u define at the ' ' path your container and define also children: [ { path: ' ', component: UserProfileComponent }, { path: 'DiscoverPageComponent ', component: DiscoverPageComponent }, ] this setup will work. Commented Apr 27, 2020 at 13:57
  • @piyushjain now anything opens the DiscoverPageComponent but nothing opens the UserPageComponent :) Commented Apr 27, 2020 at 13:57
  • @MikeS. I have added a stackblitz Commented Apr 27, 2020 at 14:24

1 Answer 1

1

Try the below sample code, do changes in your routing modules

In app-routing.moudle.ts:

const routes: Routes = [
  {
    path: '',
    loadChildren: () => import('./platform/platform.module').then(m => m.PlatformModule)
  },
];

In platform-routing.module.ts:

const routes: Routes = [
  {
    path: "user",
    children: [
      {
        path: ":user",
        component: UserProfileComponent
      }
    ]
  },
  {
    path: "discover",
    component: DiscoverComponent
  },
  {
    path: "",
    redirectTo: "discover",
    pathMatch: "full"
  }
];
  1. http://localhost:4200/discover
  2. http://localhost:4200/user/1
Sign up to request clarification or add additional context in comments.

2 Comments

I also suggest naming the user route something like user/:user instead of just :user.
I can not change the user route. instagram.com/example just looks better than instagram.com/user/example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.