简体   繁体   English

使用Angular 4的Bootstrap 4折叠导航栏

[英]Bootstrap 4 collapse navbar with Angular 4

I want to make a navbar using Bootstrap 4 and Angular 2 that collapses his content on a button when the width of the browser reaches some value. 我想使用Bootstrap 4和Angular 2创建一个导航栏,当浏览器的宽度达到某个值时,它会在按钮上折叠他的内容。 I achieve this, and when the navbar collapses I can see the button that encloses the elements in the navbar, but this button doesn't work. 我实现了这一点,当导航栏崩溃时,我可以看到包含导航栏中元素的按钮,但是这个按钮不起作用。 How can I fix this without using jquery? 如何在不使用jquery的情况下解决这个问题? I was trying to use ng-bootstrap. 我试图使用ng-bootstrap。

In the image you can see the button when the navbar collapses. 在图像中,您可以在导航栏折叠时看到按钮。

在此输入图像描述

My code so far: 我的代码到目前为止:

<nav class="navbar navbar-expand-md fixed-top navbar-dark custom-nav">
  <span class="navbar-brand mb-0 h1">Partes vehículos</span>

  <button class="navbar-toggler" type="button">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse">
    <div class="navbar-nav ml-auto">
      <a class="nav-item nav-link active" *ngIf="authService.isLoggedIn()">
        Usuario: {{ authService.currentUser.name }}
      </a>
      <a class="nav-item nav-link active" *ngIf="authService.isLoggedIn()">
        Perfil: TODO
      </a>
      <button class="nav-item btn btn-outline-warning ml-3 mr-3">Más opciones</button>
      <a class="nav-item nav-link active" routerLink="/" (click)="authService.logout()" *ngIf="authService.isLoggedIn()">
        Salir
        <span class="sr-only">(current)</span>
      </a>
    </div>
  </div>
</nav>

This stackblitz shows how you can implement the Bootstrap navbar with the ngbCollapse directive from ng-bootstrap, without using jQuery. 这个stackblitz展示了如何使用ng-bootstrap中的ngbCollapse指令实现Bootstrap导航栏,而不使用jQuery。 The boolean flag isNavbarCollapsed is defined in the component class and its value is toggled by the button. 布尔标志isNavbarCollapsed在组件类中定义,其值由按钮切换。

<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
  ...
  <button class="navbar-toggler" type="button" (click)="isNavbarCollapsed = !isNavbarCollapsed">
      <span class="navbar-toggler-icon"></span>
  </button>
  <div [ngbCollapse]="isNavbarCollapsed" class="navbar-collapse">
    ...
  </div>        
</nav>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM