7

I am confused what's wrong with this routing...

app.module.routing:

...
  {
    path: 'path1',
    canActivate: [Path1Guard],
    loadChildren: './path1/path1.module#Path1Module',
  },

path1.routing.module:

const path1Routes: Routes = [
  {
    path: '',
    component: AuthenticatedLayoutComponent,
    children: [
      {
        path: '',
        pathMatch: 'full',
        component: Path1Component,
      },
    ],
   },
 ];

As far as I understand, navigating to /path1 should result here in AuthenticatedLayoutComponent being loaded, and then Path1Component being loaded in the router outlet of AuthenticatedLayoutComponent.

If I make the path more specific:

  const path1Routes: Routes = [
  {
    path: '',
    component: AuthenticatedLayoutComponent,
    children: [
      {
        path: 'subpath1',
        pathMatch: 'full',
        component: Path1Component,
      },
    ],
   },
 ];

... and navigate to /path1/subpath1, that is exactly what happens. Without the subpath, though, the Layout component loads but loads nothing in the router outlet. So what's wrong with the way I have it? Thanks.

4
  • Did you ever find a solution to this? I am also not able to get a child component to load if its path is empty and it sits inside a parent route defined with an empty path. Commented Oct 29, 2018 at 14:42
  • I had the same problem, and found a solution: stackoverflow.com/questions/53048807/… Commented Nov 8, 2018 at 17:44
  • @blomster If I understand your solution, you are suggesting to load the layout in the lazy load in the app.routing.module? Although this is old news and I've forgotten the details, I believe that is the approach I was trying to get away from as this specific module had routes requiring two different layouts. But thanks, I believe in the context of this question your solution works. Commented Nov 9, 2018 at 18:48
  • strangely now, I have another project, and I've just created routes with an empty child inside empty parent path, and it just worked. Strange. Commented Nov 20, 2018 at 8:24

2 Answers 2

3

I believe your problem will be solved if you set the configuration like this

const path1Routes: Routes = [
  {
     path: 'path1',
     component: AuthenticatedLayoutComponent,
     children: [
      {
        path: '',
        pathMatch: 'full',
        component: Path1Component,
      },
    ],
   },
 ];

You use empty paths when you want to instantiate a component without the need for defining a new url route.

If you leave both empty it would probably just try to resolve to the first one.

By setting the configuration as above it will recognise you are on the route you defined previously on the lazy loading and then it will load the child component because it will match the empty path

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

3 Comments

This didn't work. I believe the path1 segment was already consumed by the app.routing.module. Thanks, though!
Mmmm. I will have a look and do some tests. I might have missed something. I’ll come back to you :)
I had a similar case, my 'home' route was an empty string ('') and without pathMatch it would enter a loop where my route guard always tried to redirect to ''. Adding pathMatch to full solved the issue.
1

All you need to do is remove pathMatch: 'full'

2 Comments

This didn't work. Shouldn't it be pathMatch: 'full'? I am trying to dictate that that component is loaded only if the complete path is /path1
PathMatch isn't needed. Weird, I was almost sure I've done the same before but turns out I had one more level of lazyloading in between. This may even be an issue in Router. Is it out of the question to move AuthenticatedLayoutComponent to AppModule as path1 and load those children into that there? Or moving path1Component to another lazy module would be the other workaround...:)

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.