Html – Using router-outlet in Angular 2

angularhtmltypescript

Below pseudo code describes my component tree structure.

<app-root>  

<router-outlet name="main"> 

     <Home-component>       

        <a routerLink="about">About</a> //
        <router-outlet name="home"> </<router-outlet>

     </Home-component>

</router-outlet>

</app-root>

When user clicks "About" link , the About Component is displayed in
"main" route-outlet , but my intention is to display it in "home" router-outlet,
what needs to be modified to achieve that.

"app-root" component and "Home-component" are part of root module and "AboutComponent" is part of a feature module.

Root Module routing is as below..

RouterModule.forRoot([
    {path:'' , component:LoginComponent },
    {path:'home' , component:HomeComponent}
  ]),

and Feature module routing is as below…

RouterModule.forChild([
    {path:'about' , component:AboutComponent }
])

Best Answer

follow this pattern for your routes.

 export const routes: Route[] = [{
        path: '',
        redirectTo: "login",
        pathMatch: "full"
    }, 
    {
        path: 'login',
        component: LoginComponent
    }, 
    {
        path: 'csvtemplate',
        component: TemplateComponent,
        canActivate: ['canActivateForLoggedIn'],
        children: [{
            path: '',
            redirectTo: 'dashboard'
        }, 
        {
            path:'dashboard',
            component: DashboardComponent
        },
        {
            path: 'csvtimeline',
            component: CsvTimelineComponent
        }, {
            path: 'addcategory',
            component: CsvAddCategoryComponent
        }
        ]
    }];
Related Topic