0

I have routing defined as so in my app-routing.module.ts

[
   {path: 'xyz', loadChildren: './child/child.module#ChildModule'}
]

and in child-routing.module.ts as

[
    {path: '', component: ChildComponent},
    {path: ':id', component: ChildComponent}
]

When I visit the route localhost:4200/xyz it loads the ChildComponent which is expected.

But when I go to localhost:4200/ it loads the ChildComponent. This wasn't expected as I assumed it is parent_route i.e xyz plus child_route.

How do I restrict angular from loading ChildComponent on /

Example posted on StackBlitz https://stackblitz.com/edit/angular-xts7bc

3
  • 1
    Post a complete minimal example, as a stackblitz, reproducing the issue. My guess is that you imported the child module in your root module. Commented Mar 30, 2019 at 6:47
  • @JBNizet Have updated the question with stackblitz example Commented Mar 30, 2019 at 7:38
  • 1
    Just as I suspected: you're importing your child module in parent module. Commented Mar 30, 2019 at 7:40

1 Answer 1

2

You are trying to lazy-load the ParentModule and the ChildModule, but you have imported both of them, in AppModule and ParentModule respectively. The router are not matching to the defined routes in Parent and AppModule, but they match the ChildModule, hence you get that component even on the root ('/').

To fix this, remove the ParentModule from the imports array of AppModule and remove ChildModule from the imports of ParentModule.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. It says can't find the module.
Well, that was weird. I restarted ng serve and everything is fine now. @Sachin

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.