Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with multiple layouts using route data in Angular

Fellow developers,

I've got an app that makes use of multiple layouts (eg. the login route has no navbar, but dashboard routes have a navbar + footer). What I'm doing is adding the layout to use in the data property in the route definition, so for example:

const routes: Routes = [
  {
    path: 'login',
    component: LoginComponent,
    data: { layout: Layouts.FooterOnly }
  },
];

The layout to use is then set in the AppComponent on route changes. Something strange is happening though and it's a bit tough to explain exactly what's going wrong. The bug can be reproduced by doing the following in the sample repo below:

  1. Navigate to Login route from Dashboard route.
  2. Navigate back to Dashboard route.
  3. Navigate to Users route.

For some reason, the router outlet then stops working and always displays the content of the Dashboard page (ie. dashboard works!). There's no errors and I can't pinpoint why this is happening. Can someone shed some light on how on Earth to handle this?

Sample repo with the issue: https://stackblitz.com/edit/routing-layouts-xv1s8k

like image 444
L. Berger Avatar asked Apr 26 '26 06:04

L. Berger


1 Answers

because you have two router-outlet

you can put your router outlet in an ng-template and pass it to your layout components

Here 's your repo adapted https://stackblitz.com/edit/routing-layouts-jzgekp

like image 166
JiBi Avatar answered Apr 28 '26 04:04

JiBi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!