I would like to say, please, use the name router-outlet , which works great, but the problem for me is that such URLs are not at all user friendly, and I personally think that the URL with the exit name does not make sense,
eg: -
route
{ path : "forgotPassword", component :ForgotPassword , outlet : "notlogedin" }
output to the address bar of the browser
http: // localhost: 4200 / (notlogedin: forgotPassword)
let's see how stupid it looks in the address bar.
therefore, if you try to use *ngIf to conditionally disable and enable router-outlet to solve the problem, this will not work. One router-outlet will be registered, and no matter what you do, the next router-outlet will not respond to route changes.
So here is the solution
Using ngTemplateOutlet and ng-template , we can provide a good solution to this problem, preserving only one router-outlet , see the code example below.
<ul> <li><a routerLink="/login">login</a></li> <li><a routerLink="/dashboard">dashboard</a></li> </ul> <div class="logedIn-route" *ngIf="routerActive"> <ng-container *ngTemplateOutlet="template"></ng-container> </div> <div class="logedout-route" *ngIf="!routerActive"> <ng-container *ngTemplateOutlet="template"></ng-container> </div> <ng-template #template> <router-outlet> </router-outlet> </ng-template>
try the violin
https://jsfiddle.net/imal/e4tyqv95/36/
source share