如何包括Bootstraps JS依赖项

时间:2019-05-05 13:48:53

标签: angular bootstrap-4

我想在我的Angular 7应用程序中包含Bootstrap 4。我做了npm install bootstrap --save并且CSS样式工作正常,但是当我想使用按钮作为自举程序折叠的导航栏时,该按钮没有反应。我认为这是因为Bootstrap在这种情况下取​​决于jquery和popper.js。我尝试通过npm install jquery --savenpm install popper.js --save手动添加它们。但是即使在那之后,我也无法在node_modules文件夹中找到这些模块,也不知道如何包含这些依赖项。

我们将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:2)

这有效

  

检查 WORKING STACKBLITZ

执行npm安装:〜 npm i bootstrap jquery --save

  

选项1

     

angular.json中添加以下内容:〜

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

选项2

     

在您的index.html

中添加以下链接和脚本
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

还有我的package.json供参考

{
  "name": "angular-bootstrap-setup",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "bootstrap": "^4.3.1",
    "core-js": "^2.5.4",
    "jquery": "^3.4.1",
    "popper": "^1.0.1",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.13.0",
    "@angular/cli": "~7.3.8",
    "@angular/compiler-cli": "~7.2.0",
    "@angular/language-service": "~7.2.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.2.2"
  }
}

添加node_modules屏幕截图仅供参考

node_modules folder structure

希望这会有所帮助!