Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using router-outlet in Angular 2

Tags:

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 }
])
like image 398
refactor Avatar asked Nov 21 '16 05:11

refactor


People also ask

What is the use of router outlet in Angular?

The Router-Outlet is a directive that's available from the router library where the Router inserts the component that gets matched based on the current browser's URL. You can add multiple outlets in your Angular application which enables you to implement advanced routing scenarios.

Can we use multiple router outlet in Angular 2?

Yes you can as said by @tomer above. i want to add some point to @tomer answer. firstly you need to provide name to the router-outlet where you want to load the second routing view in your view. (aux routing angular2.)

How many router outlets can be used in an Angular application?

There is no limit in angular but there are some best practices of using multiple place one in app.

Can we have more than one router outlet in Angular?

Descriptionlink Using named outlets and secondary routes, you can target multiple outlets in the same RouterLink directive.


2 Answers

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
        }
        ]
    }];
like image 94
Amit kumar Avatar answered Oct 08 '22 09:10

Amit kumar


1: Don't nest content between < router-outlet> & < /router-outlet> "router-outlet" is kind of an i-frame in functionality. It makes no sense putting content there, and have seen zero tutorials that do so.

2: If you have: < router-outlet name="MAIN_OUTLET_ID" >

Then you need something like this in app.module.ts

const appRoutes: Routes=[
 {path: 'main', outlet:MAIN_OUTLET_ID, component:MainComponent},
 /// ... more routes here ... ///
]

@NgModule({
...
imports: [
    RouterModule.forChild([ appRoutes ]),
    /// ... other imports here ... ///
],
...
})

NOTES:

1: I use "MAIN_OUTLET_ID" in my example because it disambiguate the path from the id of the router-outlet.

like image 23
twitchdotcom slash KANJICODER Avatar answered Oct 08 '22 10:10

twitchdotcom slash KANJICODER