angular - ng2-bootstrap 在 Angular 2 中不起作用 ('alert' 不是已知元素 :)

标签 angular ng2-bootstrap

我通过以下方式在运行 Angular 2.0.1 的项目上安装了 ng2-bootstrap:

npm install ng2-bootstrap --save

我的项目是这样设置的:

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

和:

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

和:

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

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

    ],
    providers: [

    ]
})
export class ClientModule { }

最后:

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

    }
}

但是在浏览网站时出现以下错误:

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

我显然在这里做错了什么,但是什么?

最佳答案

dilvish.john 的答案对我有用,除了我必须在我的 component.ts 中放置

从'ng2-bootstrap/alert'导入{AlertModule};

但我不明白背后的逻辑:为什么在 app.module 中,我们必须导入“ng2-bootstrap/ng-2bootstrap”,而在组件中我们必须导入“ng2-bootstrap/alert”?

1- 自从我们将 Ng2BootstrapModule 导入到 app.module 后,它是否可用于所有组件?因此 AlertModule 应该在不明确指定的情况下可用

2- 如果我们应该在组件中明确地这样做,我们不应该使用 从“Ng2BootstrapModule/alert”导入?

谢谢,

关于angular - ng2-bootstrap 在 Angular 2 中不起作用 ('alert' 不是已知元素 :),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40403016/

相关文章:

angular - 如何获取 Angular 5 中的版本

javascript - 使用 Javascript 客户端库作为 Angular 2 服务的 Google API 端点

ng2-bootstrap 工具提示内容在特定位置换行

Angular 2 : get image from file upload

angular - 在 angular2-rc.7 webpack 中导入 ng2-select

html - 当 ViewContainerRef 被注入(inject)指令时,它绑定(bind)到什么元素?

angular - 如何使子路线在 Angular 嵌套 route 占据整页

Angular 垫选择列表在搜索输入时无法按预期工作

html - 使用 ng2-bootstrap 图像在轮播内没有响应

angular - 无法将 moment.js 或 ng2-bootstrap 添加到 angular2 项目