Attempting to hide the header for only one specific route
Imagine having three different routes: route1
, route2
, and route3
.
In this scenario, there is a component named app-header
.
The goal is to make sure that the app-header
component is hidden when the user navigates to route1
, while being visible on the other two routes.
Despite coming across various similar questions on stackoverflow, none of the solutions seemed to work
Seeking some assistance from fellow developers!
Provided below is the relevant code snippet:
app.component.ts
export class AppComponent implements OnInit { showHeader = true; constructor( private router: Router ) { this.router.events.subscribe(event => this.modifyHeader(event)); } ngOnInit() { } modifyHeader(location) { // This method is called many times console.log(location.url); // This prints a loot of routes on console if (location.url === '/route1') { this.showHeader = false; } else { this.showHeader = true; } } }
app.component.html
<app-header *ngIf="showHeader"></app-header> <router-outlet></router-outlet>
Angular version used: 6.1.4