如何在Angular项目中使用Bootstrap?

use*_*018 166 twitter-bootstrap typescript twitter-bootstrap-3 angular

我正在开始我的第一个Angular应用程序,我的基本设置已完成.

如何将Bootstrap添加到我的应用程序中?

如果你能提供一个例子,那将是一个很大的帮助.

小智 115

如果您使用Angular-CLI生成新项目,还有另一种方法可以在Angular 2/4中访问引导程序.

  1. 通过命令行界面导航到项目文件夹.然后使用npm来安装bootstrap :
    $ npm install --save bootstrap. 该--save选项将使引导程序出现在依赖项中.
  2. 编辑.angular-cli.json文件,该文件用于配置项目.它在项目目录中.添加对"styles"数组的引用.引用必须是使用npm下载的引导程序文件的相对路径.在我的情况下它是:"../node_modules/bootstrap/dist/css/bootstrap.min.css",

我的例子.angular-cli.json:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "bootstrap-test"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "../node_modules/bootstrap/dist/css/bootstrap.min.css",
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "component": {}
  }
}
Run Code Online (Sandbox Code Playgroud)

现在bootstrap应该是默认设置的一部分.

  • 我使用步骤1安装了bootstrap,然后插入@import"../node_modules/bootstrap/dist/css/bootstrap.min.css"; 在我的src/styles.css文件中,如以下文章/sf/answers/2803793541/中所述.我不需要这篇文章的第2步,我正在使用@ angular/cli:1.3.1 (3认同)

Thi*_*ier 77

通过ng2-bootstrap项目也可以与Angular2集成:https://github.com/valor-software/ng2-bootstrap.

要安装它,只需将这些文件放在主HTML页面中:

<script src="https://cdnjs.cloudflare.com/ajax/libs/ng2-bootstrap/x.x.x/ng2-bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过这种方式将其用于组件:

import {Component} from 'angular2/core';
import {Alert} from 'ng2-bootstrap/ng2-bootstrap';

@Component({
  selector: 'my-app',
  directives: [Alert],
  template: `<alert type="info">ng2-bootstrap hello world!</alert>`
})
export class AppComponent {
}
Run Code Online (Sandbox Code Playgroud)

  • 新的angular2在@component中没有指令 (6认同)

use*_*572 38

您需要做的就是在index.html文件中包含boostrap css.

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
Run Code Online (Sandbox Code Playgroud)


pko*_*rce 20

另一个非常好(更好?)的替代方案是使用由angular-ui团队创建的一组小部件:https://ng-bootstrap.github.io


occ*_*asl 15

如果您打算使用此线程中提到的angular-ui团队的ng-bootstrap所需的Bootstrap 4 ,您将需要使用它(注意:您不需要包含JS文件):

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
Run Code Online (Sandbox Code Playgroud)

假设您正在使用SASS,您还可以在运行后npm install bootstrap@4.0.0-alpha.6 --save通过指向文件中的文件在本地引用它styles.scss:

@import '../../node_modules/bootstrap/dist/css/bootstrap.min.css';
Run Code Online (Sandbox Code Playgroud)


luk*_*uke 10

最流行的选择是使用一些分发为npm包的第三方库,如ng2-bootstrap项目https://github.com/valor-software/ng2-bootstrap或Angular UI Bootstrap库.

我个人使用ng2-bootstrap.有许多方法可以配置它,因为配置取决于Angular项目的构建方式.下面我发布了基于Angular 2 QuickStart项目https://github.com/angular/quickstart的示例配置

首先,我们在package.json中添加依赖项

    { ...
"dependencies": {
    "@angular/common": "~2.4.0",
    "@angular/compiler": "~2.4.0",
    "@angular/core": "~2.4.0",

    ...

    "bootstrap": "^3.3.7",
    "ng2-bootstrap": "1.1.16-11"
  },
... }
Run Code Online (Sandbox Code Playgroud)

然后我们必须将名称映射到systemjs.config.js中的正确URL

(function (global) {
  System.config({
    ...
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      //bootstrap
      'moment': 'npm:moment/bundles/moment.umd.js',
      'ng2-bootstrap': 'npm:ng2-bootstrap/bundles/ng2-bootstrap.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },
...
  });
})(this);
Run Code Online (Sandbox Code Playgroud)

我们必须在index.html中导入bootstrap .css文件.我们可以从我们硬盘上的/ node_modules/bootstrap目录(因为我们添加了bootstrap 3.3.7依赖)或从web获取它.我们从网上获取它:

<!DOCTYPE html>
<html>
  <head>
    ...
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    ...
  </head>

  <body>
    <my-app>Loading...</my-app>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我们应该从/ app目录编辑我们的app.module.ts文件

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

//bootstrap alert import part 1
import {AlertModule} from 'ng2-bootstrap';

import { AppComponent }  from './app.component';


@NgModule({
  //bootstrap alert import part 2
  imports:      [ BrowserModule, AlertModule.forRoot() ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

最后是来自/ app目录的app.component.ts文件

import { Component } from '@angular/core';

@Component({
    selector: 'my-app',
    template: `
    <alert type="success">
        Well done!
    </alert>
    `
})

export class AppComponent {

    constructor() { }

}
Run Code Online (Sandbox Code Playgroud)

然后我们必须在运行我们的应用程序之前安装我们的bootstrap和ng2-bootstrap依赖项.我们应该去我们的项目目录并输入

npm install
Run Code Online (Sandbox Code Playgroud)

最后,我们可以启动我们的应用

npm start
Run Code Online (Sandbox Code Playgroud)

ng2-bootstrap项目github上有许多代码示例,展示了如何将ng2-bootstrap导入到各种Angular 2项目构建中.甚至有plnkr样本.还有关于Valor软件(图书馆作者)网站的API文档.


Mic*_*zos 9

在angular-cli环境中,我发现最直接的方法如下:


1.使用scss样式表的环境中的解决方案

npm install bootstrap-sass —save
Run Code Online (Sandbox Code Playgroud)

在style.scss中:

$icon-font-path: '~bootstrap-sass/assets/fonts/bootstrap/';
@import '~bootstrap-sass/assets/stylesheets/bootstrap';
Run Code Online (Sandbox Code Playgroud)

注意1:该~字符是对nodes_modules文件夹的引用.
注2:由于我们使用scss,我们可以自定义我们想要的所有boostrap变量.


2.在具有css样式表的环境中的解决方案

npm install bootstrap —save
Run Code Online (Sandbox Code Playgroud)

在style.css中:

@import '~bootstrap/dist/css/bootstrap.css';
Run Code Online (Sandbox Code Playgroud)


UHD*_*nte 6

有几种方法可以使用Bootstrap配置angular2,其中一个最完整的方法就是使用ng-bootstrap,使用这个配置我们给algular2完全控制我们的DOM,避免使用JQuery和Boostrap .js文件.

1 - 以Angular-CLI创建的新项目为起点,并使用命令行安装ng-bootstrap:

npm install @ng-bootstrap/ng-bootstrap --save
Run Code Online (Sandbox Code Playgroud)

注意:更多信息,访问https://ng-bootstrap.github.io/#/getting-started

2 - 然后我们需要为css和网格系统安装bootstrap.

npm install bootstrap@4.0.0-beta.2 --save
Run Code Online (Sandbox Code Playgroud)

注意:更多信息,访问https://getbootstrap.com/docs/4.0/getting-started/download/

现在我们已经设置了环境,为此我们必须更改.angular-cli.json添加到样式:

"../node_modules/bootstrap/dist/css/bootstrap.min.css"
Run Code Online (Sandbox Code Playgroud)

这是一个例子:

"apps": [
    {
      "root": "src",
        ...
      ],
      "index": "index.html",
       ...
      "prefix": "app",
      "styles": [
        "../node_modules/bootstrap/dist/css/bootstrap.min.css",
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ]
Run Code Online (Sandbox Code Playgroud)

下一步是将ng-boostrap模块添加到我们的项目中,为此我们需要添加app.module.ts:

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
Run Code Online (Sandbox Code Playgroud)

然后将新模块添加到应用程序应用程序:NgbModule.forRoot()

例:

@NgModule({
  declarations: [
    AppComponent,
    AppNavbarComponent
  ],
  imports: [
    BrowserModule,
    NgbModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
Run Code Online (Sandbox Code Playgroud)

现在我们可以在angular2/5项目上开始使用bootstrap4了.