简体   繁体   English

Angular2 ng-bootstrap-没有提供程序错误

[英]Angular2 ng-bootstrap - no provider error

I am trying to integrate ng bootstrap UI into my Angular 2 project. 我正在尝试将ng bootstrap UI集成到我的Angular 2项目中。 After following the simple instructions found here: https://ng-bootstrap.github.io/#/getting-started i get the following error when using any ngb bootstrap tag: 按照此处找到的简单说明进行操作后: https ://ng-bootstrap.github.io/#/getting-started在使用任何ngb bootstrap标签时,出现以下错误:

Error: No provider for NgbAlertConfig! 错误:没有NgbAlertConfig的提供者!

app.module.ts 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 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 app.component.html

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

angular-cli.json angular-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
      }
    }

控制台错误

import NgbModule in your app.module.ts like this- 像这样在您的app.module.ts中导入NgbModule-

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

and add it in imports section of ngmodule- 并将其添加到ngmodule-

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

See if this helps. 看看是否有帮助。

对于在其他组件上遇到此错误的其他任何人,例如ngbDropdown ..,请确保在父元素上设置了ngbDropdown

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM