Bootstrap 4.1.1 Navbar not working with Angular 6

試著忘記壹切 提交于 2019-12-07 05:53:55

问题


I have been trying to work on Navbar with collapse menu and dropdown link using Bootstrap 4.1.1 and Angular 6 but somehow Navbar is not working at all. I can see elements correctly but they don't function as intended.

I installed Bootstrap via Angular CLI using 'ng install bootstrap --save' and Bootstrap dependencies: 'ng install jquery --save' and 'ng install popper.js --save'

I also included Bootstrap, jquery and popper.js in package.json and angular.json

package.json:

"dependencies": {
  "@angular/animations": "^6.0.0",
  "@angular/common": "^6.0.0",
  "@angular/compiler": "^6.0.0",
  "@angular/core": "^6.0.0",
  "@angular/forms": "^6.0.0",
  "@angular/http": "^6.0.0",
  "@angular/platform-browser": "^6.0.0",
  "@angular/platform-browser-dynamic": "^6.0.0",
  "@angular/router": "^6.0.0",
  "bootstrap": "^4.1.1",
  "core-js": "^2.5.4",
  "jquery": "^3.3.1",
  "popper.js": "^1.14.3",
  "rxjs": "^6.0.0",
  "zone.js": "^0.8.26"
},
"devDependencies": {
  "@angular-devkit/build-angular": "~0.6.0",
  "@angular/cli": "~6.0.0",
  "@angular/compiler-cli": "^6.0.0",
  "@angular/language-service": "^6.0.0",
  "@types/jasmine": "~2.8.6",
  "@types/jasminewd2": "~2.0.3",
  "@types/jquery": "^3.3.2",
  "@types/node": "~8.9.4",
  "codelyzer": "~4.2.1",
  "jasmine-core": "~2.99.1",
  "jasmine-spec-reporter": "~4.2.1",
  "karma": "~1.7.1",
  "karma-chrome-launcher": "~2.2.0",
  "karma-coverage-istanbul-reporter": "~1.4.2",
  "karma-jasmine": "~1.1.1",
  "karma-jasmine-html-reporter": "^0.2.2",
  "protractor": "~5.3.0",
  "ts-node": "~5.0.1",
  "tslint": "~5.9.1",
  "typescript": "~2.7.2"
}

angular.json:

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

html:

<nav class="navbar navbar-expand-md navbar-light bg-light">
    <a href="#" class="navbar-brand">RecipeBook</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsing-nav-bar" aria-controls="collapsing-nav-bar"
        aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="collapsing-nav-bar">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item">
                <a href="#" class="nav-link">Recipes</a>
            </li>
            <li class="nav-item">
                <a href="#" class="nav-link">Shopping list</a>
            </li>
        </ul>
        <ul class="navbar-nav">
            <li class="nav-item dropdown">
                <a href="#" class="nav-link dropdown-toggle" id="dropdownMenu" data-toggle="dropdown" 
                aria-haspopup="true" aria-expanded="false">
                    Manage
                </a>
                <div class="dropdown-menu" aria-labelledby="dropdownMenu">
                    <a href="#" class="dropdown-item">Save</a>
                    <a href="#" class="dropdown-item">Fetch</a>
                </div>
            </li>
        </ul>
    </div>
</nav>

I prefer using Bootstrap native library and I rather not use ng-bootstrap nor hacks like including CDNs in head-tag.

Am I doing something wrong here ? Any advice on how to solve this would be appreciated.


回答1:


just add umd directries popper in script.

 "./node_modules/popper.js/dist/umd/popper.min.js",



回答2:


I have lost 2 hours chasing ghosts. Notice that there is two scripts segment in angular.json. One under test and one under build. You should add under build segment. I was adding under test so angular didn't see bootstrap, and popper js

"architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/myBootstrapTestApp",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.js",
              "node_modules/popper.js/dist/umd/popper.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.js"]
          },



回答3:


path to popper should be : node_modules/popper.js/dist/umd/

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


来源:https://stackoverflow.com/questions/50740093/bootstrap-4-1-1-navbar-not-working-with-angular-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!