5

我一直在尝试使用 Bootstrap 4.1.1 和 Angular 6 在带有折叠菜单和下拉链接的导航栏上工作,但不知何故,导航栏根本不起作用。我可以正确看到元素,但它们没有按预期运行。

我使用'ng install bootstrap --save'和Bootstrap依赖项通过Angular CLI安装了Bootstrap:'ng install jquery --save'和'ng install popper.js --save'

我还在 package.json 和 angular.json 中包含了 Bootstrap、jquery 和 popper.js

包.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"
}

角.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>

我更喜欢使用 Bootstrap 本机库,我宁愿不使用 ng-bootstrap 也不喜欢在 head-tag 中包含 CDN 之类的技巧。

我在这里做错了吗?任何有关如何解决此问题的建议将不胜感激。

4

5 回答 5

3

我已经失去了 2 个小时的追逐鬼魂。请注意,angular.json 中有两个脚本段。一个正在测试中,一个正在构建中。您应该在构建段下添加。我在测试中添加,所以角度看不到引导程序和 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"]
          },
于 2018-06-21T13:06:51.583 回答
2

只需在脚本中添加umd目录 popper。

 "./node_modules/popper.js/dist/umd/popper.min.js",
于 2018-06-15T11:57:43.013 回答
1

popper 的路径应该是: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"
        ]
于 2018-08-19T16:57:12.813 回答
0

一段时间后,我只将引导库放在 jquery 和 popper 顺序下面...

"styles": [
              "src/styles.scss",
              "node_modules/bootstrap/dist/css/bootstrap.min.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",
              "node_modules/fontawesome/index.js"
            ]
于 2020-11-23T01:35:51.290 回答
0

我必须将此添加到 package.json

"dependencies": {
   "core-js": "^2.5.4",
   "jquery": "^3.3.1",
   "popper.js": "^1.14.3",

}
"devDependencies": {    
    "@types/jquery": "^3.3.2",
}

这也是 angular.json

"architect": {
        "build": {
            "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"

            ]
          },

确保在进行这些更改后运行 npm install。

于 2021-03-03T02:50:20.390 回答