Ich habe ng2-bootstrap in einem Projekt installiert, in dem Angular 2.0.1 ausgeführt wird:
npm install ng2-bootstrap --save
Ich habe mein Projekt so eingerichtet:
//systemjs.config.js
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'moment': 'node_modules/moment/moment.js',
'ng2-bootstrap/ng2-bootstrap': 'node_modules/ng2-bootstrap/bundles/ng2-bootstrap.umd.js',
// our app is within the app folder
app: 'app',
// angular bundles
Und:
// app.module.ts
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { Ng2BootstrapModule } from 'ng2-bootstrap/ng2-bootstrap';
import { AppComponent } from './app.component';
import { ClientModule } from './client/client.module';
@NgModule({
imports: [
Ng2BootstrapModule
],
declarations: [
AppComponent
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
providers: [
NotificationService,
{ provide: LocationStrategy, useClass: HashLocationStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule { }
Und:
// client.module.ts
import { Ng2BootstrapModule } from 'ng2-bootstrap';
@NgModule({
imports: [
Ng2BootstrapModule
],
declarations: [
],
providers: [
]
})
export class ClientModule { }
und schlussendlich:
// client-info.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { AlertComponent } from 'ng2-bootstrap/ng2-bootstrap';
@Component({
selector: 'client-info',
template: `
<div >
<alert type="success">hello</alert>
</div>
`,
styleUrls: ['app/client/client.css']
})
export class ClientInfoComponent {
constructor() {
}
ngOnInit(): void { }
ngOnDestroy(): void {
}
}
Beim Durchsuchen der Website erhalte ich jedoch die folgende Fehlermeldung:
Unbehandelte Ablehnung von Versprechungen: Fehler beim Analysieren von Vorlagen: 'alert' ist kein bekanntes Element: 1. Wenn 'alert' eine Angular - Komponente ist, stellen Sie sicher, dass es Teil dieses> Moduls ist. 2. Wenn 'alert' eine Webkomponente ist, fügen Sie "CUSTOM_ELEMENTS_SCHEMA" zu den> '@ NgModule.schemas' dieser Komponente hinzu, um diese Nachricht zu unterdrücken. ("
[FEHLER ->] hallo
Ich mache hier offensichtlich etwas falsch, aber was?
Sie app.module.ts sollte so sein. Es hilft mir.
// app.module.ts
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { Ng2BootstrapModule } from 'ng2-bootstrap/ng2-bootstrap';
import { ClientModule } from './client/client.module';
import { AlertModule } from 'ng2-bootstrap/components/alert';
@NgModule({
imports: [
Ng2BootstrapModule,
AlertModule
],
declarations: [
AppComponent
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
providers: [
NotificationService,
{ provide: LocationStrategy, useClass: HashLocationStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule { }
Und ändern Sie dazu client-info.component.ts
// client-info.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { AlertModule } from 'ng2-bootstrap/components/alert';
@Component({
selector: 'client-info',
template: `
<div >
<alert type="success">hello</alert>
</div>
`,
styleUrls: ['app/client/client.css']
})
export class ClientInfoComponent {
constructor() {
}
ngOnInit(): void { }
ngOnDestroy(): void {
}
}
Die Antwort von dilvish.john hat für mich funktioniert, außer dass ich sie in meine Komponente einfügen musste
import { AlertModule } from 'ng2-bootstrap/alert';
Aber ich verstehe die Logik dahinter nicht: Warum mussten wir im app.module den 'ng2-bootstrap/ng-2bootstrap' und in der Komponente den 'ng2-bootstrap/alert' importieren?
1- Ist das Ng2BootstrapModule nicht für alle Komponenten verfügbar, seit wir es in app.module importiert haben? Daher sollte AlertModule verfügbar sein, ohne es explizit anzugeben
2- und wenn wir es explizit in der Komponente tun sollten, sollten wir nicht import from 'Ng2BootstrapModule/alert'?
verwenden
Vielen Dank,