21

我正在尝试将 ng bootstrap UI 集成到我的 Angular 2 项目中。遵循此处找到的简单说明后: https ://ng-bootstrap.github.io/#/getting-started 在使用任何 ngb 引导标记时出现以下错误:

错误:没有 NgbAlertConfig 的提供者!

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

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

import { HighlightDirective } from './highlight.directive';
import { StockService } from './stock.service';
import { DateFormatterPipe } from './date-formatter.pipe'

import { routing } from './app.routing';
import { DashboardComponent } from './dashboard/dashboard.component'

import { CurrencyService } from './currency.service';
import { BondsDirective } from './bonds.directive';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';


@NgModule({
  declarations: [
    AppComponent,
    StocksComponent,
    HighlightDirective,
    DateFormatterPipe,
    DashboardComponent,
    BondsDirective
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    routing,
    NgbModule
  ],
  providers: [StockService, CurrencyService],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

}

app.component.html

    <ngb-alert>
        Random Message
    </ngb-alert>

角-cli.json

    {
      "project": {
        "version": "1.0.0-beta.17",
        "name": "my-prog-cli"
      },
      "apps": [
        {
          "root": "src",
          "outDir": "dist",
          "assets": "assets",
          "index": "index.html",
          "main": "main.ts",
          "test": "test.ts",
          "tsconfig": "tsconfig.json",
          "prefix": "app",
          "mobile": false,
          "styles": [
            "../node_modules/bootstrap/dist/css/bootstrap.css",
            "styles.css"
          ],
          "scripts": [
              "../node_modules/jquery/dist/jquery.js",
              "../node_modules/tether/dist/js/tether.js",
              "../node_modules/bootstrap/dist/js/bootstrap.js"
          ],
          "environments": {
            "source": "environments/environment.ts",
            "dev": "environments/environment.ts",
            "prod": "environments/environment.prod.ts"
          }
        }
      ],
      "addons": [],
      "packages": [],
      "e2e": {
        "protractor": {
          "config": "./protractor.conf.js"
        }
      },
      "test": {
        "karma": {
          "config": "./karma.conf.js"
        }
      },
      "defaults": {
        "styleExt": "css",
        "prefixInterfaces": false
      }
    }

控制台错误

4

2 回答 2

42

像这样在你的 app.module.ts 中导入 NgbModule -

import {NgbModule} from '@ng-bootstrap/ng-bootstrap';

并将其添加到 ngmodule- 的导入部分

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

看看这是否有帮助。

于 2016-10-15T05:36:14.993 回答
13

对于其他任何因其他组件而出现此错误的人,例如ngbDropdown.. 确保您ngbDropdown在父元素上有集合。

于 2017-10-08T22:57:01.463 回答