2016-07-22 66 views
42

我正在构建一个写在打字稿中的角2应用程序。它将使用bootstrap 4框架结合一些自定义主题,这可能吗?如何在角度2中使用引导程序4?

的“NG2的自举” NPM包不能正常工作,因为它不会让我用bootstrap css类,相反,它提供了自定义组件。 (http://github.com/valor-software/ng2-bootstrap

在我的angular 2项目中,我使用sass,是否可以从源代码构建引导程序4,因为它也使用sass进行样式化?

+0

你可以用它正常的方式。我不确定你的事。 'ng2-bootstrap'只提供给你组件。 – micronyks

回答

6

以使用需要的JQuery任何视觉库只是做到以下几点:

  • 添加库CSS索引;
  • 加入jQuery的js文件索引;
  • 在索引中添加库js文件;
  • 安装库定义文件(d.ts);
  • 中安装jQuery定义文件(d.ts);

如果库没有定义文件你可以:

  • 创建定义自己;
  • 创建库对象的同名的全球任何变量,只是没有得到编译器错误;
  • 使用有错误(这是最坏的选择);

现在你可以使用里面打字稿库;

7

我建议:

  • 简单,包括你的index.html
  • 的链接Bootstrap's CSS file使用ng-bootstrap - 一组专用的角2个指令对零部件在需要动态行为。这样你就不需要导入/设置jQuery,也不需要关心Bootstrap的JavaScript。
88

Bootstrap4阿尔法Angular2 CLI(也适用于Angular4 CLI)

如果您使用的是angular2 CLI /角4个CLI做以下操作:

npm i [email protected] --save 

这将增加引导4到你的项目。

接下来去你src/style.scsssrc/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"; 

对于风格。SCSS

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

如果使用SCSS,请确保您更改默认样式在.angular-cli.json到SCSS:

"defaults": { 
    "styleExt": "scss", 
    "component": {} 
    } 

引导JS组件

自举JS组件来工作,你仍然需要导入bootstrap.js.angular-cli.jsonscripts(这应该会自动发生盟友)

...  
"scripts": ["../node_modules/jquery/dist/jquery.js", 
        "../node_modules/tether/dist/js/tether.js", 
        "../node_modules/bootstrap/dist/js/bootstrap.js"], 
... 

更新2017年9月13日:自举4测试版现在使用popper.js代替tether.js。因此,根据您使用的版本,在脚本中导入tether.js(alpha)或popper.js(beta)。感谢抬起头@MinorThreat

+0

这对我有用,但我还需要将import语句添加到需要它的每个组件。例如,当我将它添加到我的styles.scss时,我的导航栏组件看起来不正确,除非我也将它导入到我的navbar.component.scss文件中。随着我的应用程序的增长,这感觉就像是一个开销,不得不将引导程序导入到每个组件样式表中。 –

+0

@MattSugden您不需要将其导入到所有组件。你是否将默认样式类型更改为scss?看到我更新的答案! – mahatmanich

+0

@MattSugden另外请注意,引导程序4的最近更新显着改变了导航风格。因此请检查您是否使用正确的html元素和类。我和一个项目有类似的问题。 – mahatmanich

37

上面的选项将工作,但是我用通过NPM这种方法:

  1. 导航到:Bootstrap 4和检索故宫命令获取
  2. 运行NPM命令从项目中的第1步即

    npm install [email protected] --save

  3. 安装上述后依赖运行以下命令故宫将安装引导模块 npm install --save @ng-bootstrap/ng-bootstrap 你可以阅读更多关于这个here

  4. 添加以下导入到app.module import {NgbModule} from '@ng-bootstrap/ng-bootstrap';并添加NgbModuleimports
  5. 您的应用程序模块将看像这样:

    import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; 
    
    @NgModule({ 
        declarations: [ 
        AppComponent, 
        WeatherComponent 
    ], 
        imports: [ 
        BrowserModule, 
        FormsModule, 
        HttpModule, 
        NgbModule.forRoot(), // Add Bootstrap module here. 
    ], 
        providers: [], 
        bootstrap: [AppComponent] 
    }) 
    
    export class AppModule { } 
    
  6. 打开角cli.json并插入一个新的进入的样式数组:

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

    <alert type="success">hello</alert> 
    

    ,或者如果您想使用纳克警报,然后放置在您的app.component.html page

    <ngb-alert [dismissible]="true">I'm a dismissible alert :) </ngb-alert> 
    
  8. 以下现在运行您的项目,您应该在浏览器中看到引导警报消息。

注意:您可以了解更多有关ng Components here

+1

我只能通过添加导入'NgbModule.forRoot()'(符合入门指南)并使用标记你好。这会显示警报(尽管我无法关闭它)。 –

+0

@AlanHay你有没有看过https://ng-bootstrap.github.io/#/components/alert - Closeable Alert? –

+0

试试这个: 警告!更好地检查自己,你看起来不太好。

2

首先转到您的项目目录,并按照如下步骤。

1)npm install --save @ng-bootstrap/ng-bootstrap(运行在节点JS命令提示此命令)

2)npm install [email protected](下一运行此)

3)import {NgbModule} from '@ng-bootstrap/ng-bootstrap'(在应用module.ts写)

4 )如果模块是根模块包括此

`@NgModule({ 
declarations: [AppComponent, ...], 
imports: [NgbModule.forRoot(), ...], 
bootstrap: [AppComponent] 
})` 

    (or) 

如果不是根模块

`@NgModule({ 
declarations: [OtherComponent, ...], 
imports: [NgbModule, ...] 
})` 

5)system.config.js写这个

`map: { 
'@ng-bootstrap/ng-bootstrap': 'node_modules/@ng-bootstrap/ng- 
bootstrap/bundles/ng-bootstrap.js', 
}` 

6)加入index.html中

`<script src="../node_modules/jquery/dist/jquery.min.js"></script> 
<script src="../node_modules/bootstrap/dist/js/bootstrap.min.js"></script> 
<link rel="stylesheet" type="text/css" 
href="../node_modules/bootstrap/dist/css/bootstrap.min.css" />` 
+0

引导程序4本身不应该包含'--save'? – Randy