Force collapse of Bootstrap 4 navbar on Angular 2 route change
Here's what I did which worked.
Open app.component.ts
Import Router and NavigationEnd:
import { Router, NavigationEnd } from '@angular/router';
Just under the imports insert the following in order to access JQuery:
declare var $: any;
Add the following into the constructor:
private router: Router
In the ngOnInit() add the following:
this.router.events.subscribe((evt) => { if (!(evt instanceof NavigationEnd)) { return; } // Scroll to the top of the page window.scrollTo(0, 0); // This closes the top nav $(document).click((event) => { const click = $(event.target); const _open = $('.navbar-collapse').hasClass('show'); if (_open === true && !click.hasClass('navbar-toggler')) { $('.navbar-toggler').click(); } }); });
I'm using NG 10 with bootstrap 5 but I know it works in previous version of both NG and Bootstrap 4.5.x
I have another Solution
Had a bit of inspiration from this post, but also some research by myself. One of the articles I read: https://medium.com/@tiboprea/build-a-responsive-bootstrap-4-navbar-in-angular-5-without-jquery-c59ad35b007
So, in this solution, it is possible to collapse by clicking on the toggle button but also when the route changes
//nav.component.html
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">
Brand
</a>
<button class="navbar-toggler" type="button" (click)="toggleNavbar()">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" [ngClass]="{ 'show': navbarOpen }">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="#">Item 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 2</a>
</li>
</ul>
</div>
</nav>
//nav.component.ts
import { Router, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs/operators';
export class NavComponent implements OnInit {
constructor(private router: Router) {}
ngOnInit(): void {
this.router.events
.pipe(filter(evt => evt instanceof NavigationEnd))
.subscribe((evt: NavigationEnd) => {
this.navbarOpen = false;
});
}
toggleNavbar() {
this.navbarOpen = !this.navbarOpen;
}
}
I was able to achieve what I wanted by triggering a click on the navbar toggle button.
In both pieces of code below, probably the main thing to pay attention to is the collapseNav()
function.
Here's my component code:
import { Component, ViewChild, ElementRef } from '@angular/core';
import { Auth } from './auth.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [Auth]
})
export class AppComponent {
@ViewChild('navbarToggler') navbarToggler:ElementRef;
constructor(private auth: Auth) {}
navBarTogglerIsVisible() {
return this.navbarToggler.nativeElement.offsetParent !== null;
}
collapseNav() {
if (this.navBarTogglerIsVisible()) {
this.navbarToggler.nativeElement.click();
}
}
}
And here's my markup.
<nav class="navbar navbar-toggleable-md navbar-inverse bg-inverse fixed-top">
<button #navbarToggler class="navbar-toggler navbar-toggler-right"
type="button"
data-toggle="collapse"
data-target="#foodie-navbar"
aria-controls="foodie-navbar"
aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" routerLink="/">Foodie</a>
<div class="collapse navbar-collapse" id="foodie-navbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item" *ngIf="auth.authenticated()">
<a (click)="collapseNav()" class="nav-link" [routerLink]="['/places']">Places</a>
</li>
<li class="nav-item">
<a (click)="collapseNav(); auth.login()" class="nav-link" routerLink="" *ngIf="!auth.authenticated()">Log In</a>
<a (click)="collapseNav(); auth.logout()" class="nav-link" routerLink="" *ngIf="auth.authenticated()">Log Out</a>
</li>
</ul>
</div>
</nav>
<div class="container">
<router-outlet></router-outlet>
</div>