Angular Bootstrap 4 Navbar无法切换,并且navbar-dark有问题

时间:2019-04-01 07:53:06

标签: html css angular twitter-bootstrap bootstrap-4

当我的屏幕宽度较小且试图单击汉堡包来切换导航时,什么也没发生。我已经尝试用已经提出的问题解决问题,但是我却找不到错误。

 另外,一旦我删除navbar-dark,汉堡包就会消失-再次将此类添加到对象中,将导航栏的颜色更改为白色,而我不希望这样做。

<nav class="navbar navbar-dark navbar-expand-lg">
  <div class="container-fluid px-0">
    <div class="row w-100">
      <div class="col-2">
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
      </div>

      <div class="col-10">
        <div class="collapse justify-content-end navbar-collapse" id="navbarTogglerDemo03">
          <ul class="navbar-nav mt-2 mt-lg-0">
            <li class="nav-item active">
              <a class="nav-link" href="#">Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">About</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Portfolio</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Book Me</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</nav>


Angular.json:

"styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "src/styles.css",
              "node_modules/aos/dist/aos.css"
            ],
            "scripts": [
              "node_modules/popper.js/dist/popper.min.js",
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
            ],

Package.json:

"dependencies": {
    "@angular/animations": "^7.2.11",
    "@angular/cdk": "^7.3.6",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/fire": "^5.1.1",
    "@angular/forms": "~7.2.0",
    "@angular/material": "^7.3.6",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "aos": "^2.3.4",
    "bootstrap": "^4.3.1",
    "core-js": "^2.5.4",
    "firebase": "^5.8.5",
    "jquery": "^3.3.1",
    "ng-recaptcha": "^4.2.1",
    "popper.js": "^1.14.7",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },

1 个答案:

答案 0 :(得分:0)

您遇到了一些问题:

  1. 将引导程序安装到角度6以上的as here
  2. bg-dark类添加到<nav>
  3. 使用此example演示角度引导行为

See here working code

HTML

<nav class="navbar navbar-dark bg-dark navbar-expand-lg">
  <div class="container-fluid px-0">
    <div class="row w-100">
      <div class="col-2">
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation" (click)="isShown = !isShown" >
          <span class="navbar-toggler-icon"></span>
        </button>
      </div>

      <div class="col-10">
        <div class="collapse justify-content-end navbar-collapse" [ngClass]="{ 'show': isShown }" id="navbarTogglerDemo03">
          <ul class="navbar-nav mt-2 mt-lg-0">
            <li class="nav-item active">
              <a class="nav-link" href="#" (click)="isShown = false" >Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#" (click)="isShown = false" >About</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#" (click)="isShown = false" >Portfolio</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#" (click)="isShown = false" >Book Me</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</nav>

TS

isShown:boolean=false;