Multiple components per route in angular
15,588
Solution 1
You can use named outlets:
const appRoutes: Routes = [
{
path: '', component: HomeComponent, children: [
{ path: 'about', component: AboutComponent },
{ path: 'clients', component: ClientsComponent },
{ path: 'services', component: ServicesComponent },
{ path: 'contact', component: ContactComponent },
{ path: 'datatable', component: DataComponent }
]
},
{ path: '', component: SidebarComponent, outlet:'secondary' }
]
HTML:
<router-outlet></router-outlet> //primary outlet
<router-outlet name="secondary"></router-outlet> //secondary outlet
Solution 2
Why not just have the HomeComponent
be the parent component and SideBarComponent
live inside HomeComponent's template?
Related videos on Youtube
Author by
SONGSTER
Updated on June 04, 2022Comments
-
SONGSTER almost 2 years
What I want to do is, I want to load the
home component
andsidebar component
at the same time.const appRoutes: Routes = [ { path: '', component: HomeComponent, children: [{ path: 'sidebar', component: SidebarComponent, children: [ { path: 'about', component: AboutComponent }, { path: 'clients', component: ClientsComponent }, { path: 'services', component: ServicesComponent }, { path: 'contact', component: ContactComponent }, { path: 'datatable', component: DataComponent } ] }] }
-
SONGSTER over 6 yearshome component is where all the other components code come together. like header footer etc. and then home component would need an outlet to load. so thats why
-
Rahul Singh over 6 yearshave a component with a empty html and just add
router-outlet
to it that solves it -
SONGSTER over 6 yearsi did that. problem is that homecomponent loads but to load the sidebar component i have to type the url in the adress bar. i want the sidebar component and home component to load simultaneously
-
Mike Tung over 6 years@SONGSTER remove sidebar from the routes and make it a child component of home component.
-
Mike Tung over 6 yearsremove it from your angular router and you are all set.
-
SONGSTER over 6 yearsgreat didnt know about this. hope this will solve the problem
-
SONGSTER over 6 yearsbut what if i want my home page to show a preselcted option from sidebar menu. How would i do that. like i want when someone lands on the home page, he should be shown home>about>mark's page
-
SONGSTER over 6 yearsor just sidebar>about page. then he could select other options if he wants to. How something like that could be acomplished
-
Ringo over 6 yearsPer your first comment, routerLinkActive will add an active class to your html. So it will show the route option you are on as active. See my updated demo.