ng2-bootstrap not working in Angular 2 ('alert' is not a known element:)

≯℡__Kan透↙ 提交于 2019-12-10 03:19:00

问题


I've installed ng2-bootstrap on a project running Angular 2.0.1 via:

npm install ng2-bootstrap --save

I've setup my project like this:

    //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

And:

// 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 { }

And:

// client.module.ts
import { Ng2BootstrapModule } from 'ng2-bootstrap';

@NgModule({
    imports: [
        Ng2BootstrapModule
    ],
    declarations: [

    ],
    providers: [

    ]
})
export class ClientModule { }

and finally:

// 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 {

    }
}

But when browsing the site I get the following error:

Unhandled Promise rejection: Template parse errors: 'alert' is not a known element: 1. If 'alert' is an Angular component, then verify that it is part of this >module. 2. If 'alert' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the >'@NgModule.schemas' of this component to suppress this message. ("

[ERROR ->]hello

I am obviously doing something wrong here but what?


回答1:


You app.module.ts should be like this.It helps me.

// 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 { }

And change client-info.component.ts to this

    // 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 {

        }

}



回答2:


The answer of dilvish.john worked for me, except that in my component.ts I had to put

import { AlertModule } from 'ng2-bootstrap/alert';

But I do not understand the logic behind: why in the app.module, we had to import the 'ng2-bootstrap/ng-2bootstrap' and in the component we had to import 'ng2-bootstrap/alert'?

1- isn't the Ng2BootstrapModule available for all components since we imported it in app.module? and so AlertModule should be available without specifying it explicitly

2- and if we should do it explicitly in the component, shouldn't we use import from 'Ng2BootstrapModule/alert'?

Thank you,



来源:https://stackoverflow.com/questions/40403016/ng2-bootstrap-not-working-in-angular-2-alert-is-not-a-known-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!