I've managed to make Bootstrap's navbar collapse successfully using the data-toggle
and data-target
attributes on each li
element.
If you're interested, here is a SO answer that explains a way to achieve this without modifying every single li
:
Below is a snippet of my navbar code featuring two of the li
elements:
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item" data-toggle="collapse" data-target=".navbar-collapse.show">
<a class="nav-link" href="#" routerLink="/servers">Servers</a>
</li>
<li class="nav-item" data-toggle="collapse" data-target=".navbar-collapse.show">
<a class="nav-link" href="#" routerLink="/servers">Variables</a>
</li>
I'm getting close to implementing this functionality in my Angular 8 Typescript file:
export class AppComponent implements OnInit {
ngOnInit(): void {
const navbarItems = document.querySelectorAll('.navbar-nav>li');
navbarItems.forEach(navbarItem => {
navbarItem.addEventListener('click', () => {
const navbar = document.querySelector('.navbar-collapse').collapse('hide');
})
});
}
}
The problem lies in the last line:
Property
collapse
does not exist on typeelement
.
What steps should I take to fix this issue? Additionally, are there any alternative methods that could work better?
I have attempted to cast navbar
as various HTML element types, but none of them seem to resolve the problem.