Skip to content
Advertisement

Is there a way I can render a 2 level nested component in Angular using `router-outlet`?

I have a sidebar with some links. The sidebar is located at the /dashboard route. The links on the sidebar are direct children to /dashboard. I now want to render the children of /dashboard inside the main router-outlet. I have no idea on how to approach this. The following are some code snippets to elaborate my question further

My routing structure

JavaScript

App component

JavaScript

Login.html

JavaScript

Login.ts

JavaScript

Workgroup Component html

JavaScript
  • When I click on the new button in the workgroup component, I want it to navigate me to the savewg component view and replace the content in the workgroup component.
  • Any suggestions on how I can tackle this will be appreciated

Advertisement

Answer

Modules are only allow one main router-outlet per module. You will have to create a separate module for workgroups path and lazy load it. The workgroups module will have its own routing file and its own router outlet where you will load all your routes from your workgroup module. See below stackblitz for a working example.

app-routing.module.ts

JavaScript

workgroups-routing.module.ts

JavaScript

workgroups.module.ts

JavaScript

Below are resources for lazy loading modules and a stackblitz example.

https://stackblitz.com/edit/angular-ivy-hbogtn

https://www.freakyjolly.com/angular-nested-routing-with-multiple-routeroutlet-using-loadchildren-having-own-router-modules-example-application/#.X3IQa3WYXmE

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement