2

I have a problem with nested children routes in angular. My routes registration:

import { Routes, RouterModule } from '@angular/router';
import { NavigationComponent } from './container/container.component';
import { Tab1Component } from './tab1/tab1.component';

const routes: Routes = [
    {
        path: 'nav',
        component: NavigationComponent,
        children: [
            {
                path: 'tab1',
                component: Tab1Component
            }
        ]
    },
    { path: '', redirectTo: 'nav', pathMatch: 'full' },
];

export const MyRouting = RouterModule.forChild(routes);

(Note this is a lazy loaded module and the base route is /section/).

I can access to NavigationComponent with this URL: http://localhost:4200/section/nav/ and it is showed correctly.

Then I just access to http://localhost:4200/section/nav/tab1 and NavigationComponent is rendered again. If I remove component: NavigationComponent, it works ok. Why?

Newbie in Angular, thanks!

1
  • here you are loading child route tab1 into parent NavigationComponent that's the expected behaviour Commented Dec 2, 2019 at 12:07

1 Answer 1

5

In your NavigationComponent

you need to add <router-outlet></router-outlet>

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

3 Comments

It works, but why? I already have a <router-outlet></router-outlet> in the general app.component. I believed that if I put another router-outlet it should be named.
you are trying to load child component (Tab1Component) inside of Parent (NavigationComponent) , in your app component <router-outlet> will load Parent and <router-outlet> inside will load Tab1Component , you can see when your inspect the page
Ok, so when I declare childs routes, the parent needs <router-outlet></router-outlet>. And I must have so many router-outlets as levels. Thanks! I have been 4 hours with this.

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.