如何将引导添加到angular cli项目

如何将引导添加到angular cli项目,angular,twitter-bootstrap,angular-cli,ngx-bootstrap,Angular,Twitter Bootstrap,Angular Cli,Ngx Bootstrap,我们希望在使用angular cli 1.0.0-beta.5(w/node v6.1.0)生成的应用程序中使用bootstrap 4(4.0.0-alpha.2) 在使用npm获得引导及其依赖项之后,我们的第一种方法是将它们添加到angular cli build.js: 'bootstrap/dist/**/*.min.+(js|css)', 'jquery/dist/jquery.min.+(js|map)', 'tether/dist/**/*.min.+(js|css)',

我们希望在使用angular cli 1.0.0-beta.5(w/node v6.1.0)生成的应用程序中使用bootstrap 4(4.0.0-alpha.2)

在使用npm获得引导及其依赖项之后,我们的第一种方法是将它们添加到
angular cli build.js

'bootstrap/dist/**/*.min.+(js|css)',  
'jquery/dist/jquery.min.+(js|map)',  
'tether/dist/**/*.min.+(js|css)',
并将它们导入我们的
index.html

<script src="vendor/jquery/dist/jquery.min.js"></script>
<script src="vendor/tether/dist/js/tether.min.js"></script>
<link rel="stylesheet" type="text/css" href="vendor/bootstrap/dist/css/bootstrap.min.css">
<script src="vendor/bootstrap/dist/js/bootstrap.min.js"></script>

这在
ng-serve
上运行得很好,但是当我们使用
-prod
标记生成一个构建时,所有这些依赖项都从
dist/vendor
中消失了(令人惊讶!)

我们打算如何在使用angular cli生成的项目中处理此类场景(即加载引导脚本?

我们有以下想法,但我们真的不知道该走哪条路

  • 使用CDN?但我们更愿意提供这些文件,以保证它们将可用

  • 在我们的
    ng构建-prod
    之后,是否将依赖项复制到
    dist/供应商
    ?但这似乎是angular cli应该提供的,因为它“负责”构建部分

  • src/system config.ts
    中添加jquery、bootstrap和tether,并以某种方式将它们拉到
    main.ts
    中的包中?但考虑到我们不打算在应用程序的代码中显式使用它们,这似乎是错误的(例如,与矩.js或类似lodash的东西不同)


重要更新:ng2引导现在由

ngx引导支持角度3和角度4

更新:1.0.0-beta.11-webpack或更高版本

首先,在终端中使用以下命令检查您的angular cli版本:

ng -v
如果您的angular cli版本大于1.0.0-beta.11-webpack,则应遵循以下步骤:

  • 安装ngx引导程序和引导程序:

    npm install ngx-bootstrap bootstrap --save
    
  • 这条生产线现在安装Bootstrap 3,但将来可以安装Bootstrap 4。请记住,ngx引导支持这两个版本

  • 打开src/app/app.module.ts并添加:

    import { AlertModule } from 'ngx-bootstrap';
    ...
    @NgModule({
    ...
    imports: [AlertModule.forRoot(), ... ],
    ... 
    })
    
    <alert type="success">hello</alert>
    
  • 打开angular.json(对于angular6和更高版本,文件名更改为angular.json),并在
    样式
    数组中插入一个新条目:

    "styles": [
    "styles.css",
    "../node_modules/bootstrap/dist/css/bootstrap.min.css"
    ],
    
  • 打开src/app/app.component.html并添加:

    import { AlertModule } from 'ngx-bootstrap';
    ...
    @NgModule({
    ...
    imports: [AlertModule.forRoot(), ... ],
    ... 
    })
    
    <alert type="success">hello</alert>
    
    然后,打开angular cli build.js并添加以下行:

    vendorNpmFiles: [
       ...
       'ngx-bootstrap/**/*.js',
       ...
    ]
    
    现在,打开您的src/system config.ts,然后写:

    const map:any = {
       ...
       'ngx-bootstrap': 'vendor/ngx-bootstrap',
       ...
    }
    
    ……和:

    const packages: any = {
      'ngx-bootstrap': {
        format: 'cjs',
        defaultExtension: 'js',
        main: 'ngx-bootstrap.js'
      }
    };
    

    更新v1.0.0-beta.26

    module.exports = function (defaults) {
    return new Angular2App(defaults, {
        vendorNpmFiles: [
            'ng2-bootstrap/**/*',
            'moment/moment.js'
        ]
    });
    };
    
    ng build
    
    npm install bootstrap@3 jquery --save
    
    您可以在文档上看到导入引导的新方法

    如果仍然无法工作,请使用命令ng serve重新启动

    1.0.0或以下版本:

    /** Map relative paths to URLs. */
    const map: any = {
      'moment': 'vendor/moment/moment.js',
      'ng2-bootstrap': 'vendor/ng2-bootstrap'
    };
    
    /** User packages configuration. */
    const packages: any = {
      'ng2-bootstrap': {
        defaultExtension: 'js'
      },
      'moment':{
         format: 'cjs'
      }
    };
    
    在index.html文件中,您只需要引导css链接(不需要js脚本)

    在安装到my_project/src/system-config.ts:

    /** Map relative paths to URLs. */
    const map: any = {
      'moment': 'vendor/moment/moment.js',
      'ng2-bootstrap': 'vendor/ng2-bootstrap'
    };
    
    /** User packages configuration. */
    const packages: any = {
      'ng2-bootstrap': {
        defaultExtension: 'js'
      },
      'moment':{
         format: 'cjs'
      }
    };
    
    在我的项目/angular-cli-build.js中:

    module.exports = function (defaults) {
    return new Angular2App(defaults, {
        vendorNpmFiles: [
            'ng2-bootstrap/**/*',
            'moment/moment.js'
        ]
    });
    };
    
    ng build
    
    npm install bootstrap@3 jquery --save
    
    不要忘记这个命令 要将模块放入供应商中,请执行以下操作:

    module.exports = function (defaults) {
    return new Angular2App(defaults, {
        vendorNpmFiles: [
            'ng2-bootstrap/**/*',
            'moment/moment.js'
        ]
    });
    };
    
    ng build
    
    npm install bootstrap@3 jquery --save
    

    从另一个具有相同原理的模块导入。

    查找关键字>全局库安装 on帮助您找到答案

    根据他们的文档,您可以采取以下步骤来添加引导库

    首先从npm安装引导程序:

    npm install bootstrap@next
    
    然后将所需的脚本文件添加到应用程序[0]。angular-cli.json文件中的脚本:

     "scripts": [
        "../node_modules/bootstrap/dist/js/bootstrap.js"
        ],
    
    最后将引导CSS添加到应用[0]。样式数组:

    "styles": [
        "styles.css",
        "../node_modules/bootstrap/dist/css/bootstrap.css"
        ],
    
    如果您正在运行ng serve,请重新启动它,Bootstrap 4应该可以在您的应用程序上运行。

    这是最好的解决办法。
    但如果你不重新启动ng服务,它就永远不会起作用。强烈建议您执行最后一个操作。

    您也可以观看Mike Brocchi的视频,其中提供了有关如何将引导添加到生成的项目的有用信息。
    (大约13:00分)

    首先,您必须使用这些命令安装tether、jquery和bootstrap

    npm i -S tether
    npm i -S jquery
    npm i -S bootstrap@4.0.0-alpha.4 
    
    在angular-cli.json(从版本6开始的angular.json)文件中添加这些行之后

    请执行以下操作:

    npm i bootstrap@next --save
    
    // version 3
    @import 'variables';
    @import '../node_modules/bootstrap-sass/assets/stylesheets/_bootstrap';
    
    // version 4
    @import 'variables';
    @import '../node_modules/bootstrap/scss/bootstrap';
    
    $brand-primary: red;
    
    这将为您的项目添加引导4

    接下来,转到您的
    src/style.scss
    src/style.css
    文件(选择您正在使用的文件),并在其中导入引导:

    对于style.css

    /* You can add global styles to this file, and also import other style files */
    @import "../node_modules/bootstrap/dist/css/bootstrap.min.css";
    
    对于style.scss

    /* You can add global styles to this file, and also import other style files */
    @import "../node_modules/bootstrap/scss/bootstrap";
    
    @import "../node_modules/bootstrap/dist/css/bootstrap.min.css";
    
    @import "../node_modules/bootstrap/dist/css/bootstrap.css";  //bootstrap
    @import "~@angular/material/prebuilt-themes/indigo-pink.css // angular material theme
    
    对于脚本,您仍然需要像这样将文件添加到angular-cli.json文件中(在angular版本6中,此编辑需要在文件angular.json中完成):


    我猜上述方法在发布后已经改变,请查看此链接

    启动项目

    npm i -g angular-cli
    ng new my-app
    cd my-app
    ng serve
    npm install --save @ng-bootstrap/ng-bootstrap
    
    安装ng引导和引导

    npm install ng2-bootstrap bootstrap --save
    
    打开src/app/app.module.ts并添加

    import { AlertModule } from 'ng2-bootstrap/ng2-bootstrap';
    ...
    
    @NgModule({
       ...
       imports: [AlertModule, ... ],
        ... 
    })
    
    npm install bootstrap@3
    
    打开angular-cli.json并在样式数组中插入一个新条目

    "styles": [
            "styles.css",
            "../node_modules/bootstrap/dist/css/bootstrap.min.css"
          ],
    
    打开src/app/app.component.html并通过添加来测试所有作品

    <alert type="success">hello</alert>
    
    你好
  • 在bash中运行npm安装ng2引导引导引导引导--保存
  • 在angular cli.json中添加/更改以下内容
    “样式”:[
    “./node_modules/bootstrap/dist/css/bootstrap.min.css”
    ],
    “脚本”:[
    “./node_modules/jquery/dist/jquery.min.js”,
    “./node_modules/bootstrap/dist/js/bootstrap.min.js”
    ],
  • 试试这个

    npm install bootstrap@next
    

  • 安装引导程序

    npm install bootstrap@next
    
  • 向.angular-cli.json添加代码:

    "styles": [
      "styles.css",
      "../node_modules/bootstrap/dist/css/bootstrap.css"
    ],
    "scripts": [
      "../node_modules/jquery/dist/jquery.js",
      "../node_modules/tether/dist/js/tether.js",
      "../node_modules/bootstrap/dist/js/bootstrap.js"
    ],
    
  • 最后将bootstrap.css添加到您的代码style.scss

    /* You can add global styles to this file, and also import other style files */
    @import "../node_modules/bootstrap/scss/bootstrap";
    
    @import "../node_modules/bootstrap/dist/css/bootstrap.min.css";
    
    @import "../node_modules/bootstrap/dist/css/bootstrap.css";  //bootstrap
    @import "~@angular/material/prebuilt-themes/indigo-pink.css // angular material theme
    
  • 重新启动本地服务器


  • 在相应的项目目录中打开终端/命令提示符,并键入以下命令

    npm install --save bootstrap
    
    然后打开.angular-cli.json文件,查找

    “样式”:[
    “styles.css”
    ],

    换成

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

    全部完成。

    因为还没有人提到官方(angular cli团队)关于如何包含引导的故事:
    import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
    
    @NgModule({
      declarations: [AppComponent, ...],
      imports: [NgbModule.forRoot(), ...],  
      bootstrap: [AppComponent]
    })
    export class AppModule {
    }
    
    import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
    
    @NgModule({
      declarations: [OtherComponent, ...],
      imports: [NgbModule, ...], 
    })
    export class OtherModule {
    }
    
      "styles": [
        "../node_modules/bootstrap/dist/css/bootstrap.css"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.js",
        "../node_modules/tether/dist/js/tether.js",
        "../node_modules/bootstrap/dist/js/bootstrap.js"
      ],
    
    "styles": [
       "styles.scss",
       "../node_modules/bootstrap/dist/css/bootstrap.min.css"
    ],
    
    npm install --save @ng-bootstrap/ng-bootstrap
    
    import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
    
    @NgModule({
       imports: [NgbModule.forRoot(), ...],
    })
    
    import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
    
    @NgModule({
       imports: [NgbModule, ...],
    })
    
    npm install boostrap@next --save
    
    "../node_modules/bootstrap/dist/css/bootstrap.css",
    
    @import "../node_modules/bootstrap/dist/css/bootstrap.css";  //bootstrap
    @import "~@angular/material/prebuilt-themes/indigo-pink.css // angular material theme
    
    "styles": [
       "../node_modules/bootstrap/dist/css/bootstrap.min.css",
       "styles.css"
    ],
    
    <button type="button" class="btn btn-primary">Primary</button>
    
    npm install bootstrap
    
    npm install --save popper.js
    
     "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.css",
              "src/styles.css"
            ],
    
            "scripts": [
              "node_modules/popper.js/dist/umd/popper.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
            ]
    
    npm install --save @bootsrap@4
    
    + bootstrap@4.1.3
    updated 1 package in 8.245s
    
    "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
               "src/styles.css"
              ],
    
    @import 'bootstrap/scss/bootstrap.scss';
    
    npm install bootstrap@3 jquery --save
    
    npm install --save bootstrap
    
    npm install bootstrap@3
    
    "styles": [
      "node_modules/bootstrap/dist/css/bootstrap.min.css",
      "src/styles.css"
    ],
    
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    
    
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
    
    ng add ngx-bootstrap
    
    <button type="button" class="btn btn-outline-primary">Primary</button>
    <button type="button" class="btn btn-outline-secondary">Secondary</button>
    <button type="button" class="btn btn-outline-success">Success</button>
    <button type="button" class="btn btn-outline-danger">Danger</button>
    <button type="button" class="btn btn-outline-warning">Warning</button>
    <button type="button" class="btn btn-outline-info">Info</button>
    <button type="button" class="btn btn-outline-light">Light</button>
    <button type="button" class="btn btn-outline-dark">Dark</button>
    
    npm install bootstrap@next
    
    @import '~bootstrap';