javascript - 如何在 Angular 6 项目中添加 Bootstrap ?

标签 javascript html angular angular-ui-bootstrap

<分区>

app.component.html

index.html

app.component.ts

我尝试在 angular.json 包中添加样式依赖项,但显示找不到该模块。添加两个引导文件。 这是两个文件的屏幕截图

angular.json文件是这样的angular.json file

最佳答案

对于 Angular 版本 11+

配置

angular.json 配置中的样式和脚本选项现在允许直接引用包:

之前:"styles": ["../node_modules/bootstrap/dist/css/bootstrap.css"]
之后:"styles": ["bootstrap/dist/css/bootstrap.css"]

          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/ng6",
            "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.css","bootstrap/dist/css/bootstrap.min.css"

            ],
            "scripts": [
                       "jquery/dist/jquery.min.js",
                       "bootstrap/dist/js/bootstrap.min.js"
                       ]
          },

Angular 版本 10 及以下

您使用的是 Angular v6 而不是 2

Angular v6 Onwards

angular 6 之后的 CLI 项目将使用 angular.json而不是 .angular-cli.json用于构建和项目配置。

每个 CLI 工作区都有项目,每个项目都有目标,每个目标都可以有配置。 Docs

. {
  "projects": {
    "my-project-name": {
      "projectType": "application",
      "architect": {
        "build": {
          "configurations": {
            "production": {},
            "demo": {},
            "staging": {},
          }
        },
        "serve": {},
        "extract-i18n": {},
        "test": {},
      }
    },
    "my-project-name-e2e": {}
  },
}

选项 1
执行 npm install bootstrap@4 jquery --save
Bootstrap 的 JavaScript 部分依赖于 jQuery .所以你需要 jQuery JavaScript库文件也是。

在您的 angular.json 中,将文件路径添加到 build 下的样式和脚本数组中目标
注意: 在 v6 之前,Angular CLI 项目配置存储在 <PATH_TO_PROJECT>/.angular-cli.json. 中从 v6 开始,文件的位置更改为 angular.json.由于不再有前导点,默认情况下该文件不再隐藏并且处于同一级别。
这也意味着 angular.json 中的文件路径不应包含前导点和斜杠

i.e you can provide an absolute path instead of a relative path

.angular-cli.json文件路径为 "../node_modules/"
angular.json它是 "node_modules/"

 "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/ng6",
            "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.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
将来自 CDN(内容分发网络)的文件添加到您的项目 CDN LINK

打开文件src/index.html并插入

<link> head 部分末尾的元素以包含 Bootstrap CSS 文件
一个<script>在正文部分底部包含 jQuery 的元素
一个<script>在正文部分底部包含 Popper.js 的元素
一个<script>在正文部分底部包含 Bootstrap JavaScript 文件的元素

  <!doctype html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Angular</title>
      <base href="/">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="icon" type="image/x-icon" href="favicon.ico">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    </head>
    <body>
      <app-root>Loading...</app-root>
      <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    </body>
    </html>

选项 3
执行 npm install bootstrap
src/styles.css添加以下行:

@import "~bootstrap/dist/css/bootstrap.css";

选项 4
<强> ng-bootstrap 它包含一组基于 Bootstrap 标记和 CSS 的原生 Angular 指令。因此,它不依赖于 jQuery 或 Bootstrap 的 JavaScript

npm install --save @ng-bootstrap/ng-bootstrap

安装后将其导入根模块并在 @NgModule 中注册导入`数组

import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
@NgModule({
  declarations: [AppComponent, ...],
  imports: [NgbModule.forRoot(), ...],
  bootstrap: [AppComponent]
})

注意
ng-bootstrap需要在您的项目中添加 Bootstrap 的 4 个 css。您需要通过以下方式显式安装它:
npm install bootstrap@4 --save 在您的 angular.json 中,将文件路径添加到 build 下的样式数组中目标

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

P.S 请重启您的服务器

`ng serve || npm 开始`😉

关于javascript - 如何在 Angular 6 项目中添加 Bootstrap ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50290197/

相关文章:

javascript - 创建一个勾选框,然后通过单击自动勾选所有其他勾选框,并显示一个按钮

javascript - 在不失去响应能力的情况下将数字图像固定在挂板上

javascript - jQuery toggleClass with single select on - 左键单击和右键单击

javascript - 在动态 block React 中对父元素使用 ref

javascript - 以编程方式禁用 window.location.reload?

html -::before 到底做了什么?

node.js - Angular 为 2 的 Node ,在 url 栏中输入路线时出现 404 错误

Angular 2 错误 : Loading chunk failed many times

javascript - Angular 4 - 使用 Angular 的 *ngFor 在 HTML 上打印唯一的动态值

javascript - Controller 内部的 Angular 1.6 绑定(bind)