ng-bootstrap无法在角度4中工作

Mar*_*sF8 15 twitter-bootstrap bootstrap-4 ng-bootstrap angular angular4-forms

我是角度4的新手,我正在尝试配置bootstrap.我安装了ng-bootstrap:

https://ng-bootstrap.github.io/#/getting-started

我在页面上做了所有的事情,但我在页面上看不到引导程序.这是我的代码:

SRC /应用程序/ app.module.ts

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

import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';

@NgModule({
  declarations: [
    AppComponent
  ],
imports: [
   BrowserModule,
   FormsModule,  // add  this
   NgbModule.forRoot()
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

SRC /应用程序/ app.component.html

<div class="container">

<h1 class="text-primary">{{title}} </h1>

<h2 class="text-primary">My Heroes</h2>
<ul class="heroes">
  <li *ngFor="let hero of heroes"
    [class.selected]="hero === selectedHero"
    (click)="onSelect(hero)">
    <span class="badge">{{hero.id}}</span> {{hero.name}}
   </li>
</ul>

<div class="alert alert-success" role="alert">
 <strong>Well done!</strong> You successfully read this important alert 
   message.
</div>

<div *ngIf="selectedHero">
  <h2>{{selectedHero.name}} details!</h2>
<div><label>id: </label>{{selectedHero.id}}</div>
<div>
  <label>name: </label>
  <input [(ngModel)]="selectedHero.name" placeholder="name"/>
</div>
Run Code Online (Sandbox Code Playgroud)

我在index.html中也尝试了一些代码,但它不起作用,

对于这条线,我期待看到一些颜色:

<h1 class="text-primary">{{title}} </h1>
Run Code Online (Sandbox Code Playgroud)

当我检查html的标题时,我找不到任何引导程序的引用.有什么帮助吗?谢谢

Mel*_*igy 36

在你提到的页面中,ng-bootstrap提到bootstrap CSS作为依赖项.所以这是单独加载的.

如果您使用Angular CLI创建应用程序,可以按照此官方指南添加它,

更新:

正如在评论中提到,添加CSS来styles.angular-cli.json(或任何其他更改本文件)将要求您重新启动ng serve或者ng build --watch,如果您有已经运行.

更新2:

对于Angular 6及更高版本,您可以使用此解决方案.

转到styles.css并添加此行

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

另请参阅如何在引擎盖下工作:

https://blog.angularindepth.com/this-is-how-angular-cli-webpack-delivers-your-css-styles-to-the-client-d4adf15c4975

  • 非常感谢,我需要重新启动 ng 服务,它运行良好!谢谢 (2认同)