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

时间:2016-09-15 11:17:48

标签: angular webpack ng2-bootstrap

使用webpack,我使用npm安装了ng2-select,我安装了ng2-bootstrap,工作正常。但是当我导入ng2-select

时,我的控制台出现以下错误
  

未捕获错误:意外指令' SelectComponent'由模块' AppModule'

导入

这是我的 app.module.ts 文件:

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


import { Ng2BootstrapModule } from 'ng2-bootstrap/ng2-bootstrap';
import { SELECT_DIRECTIVES } from "ng2-select/ng2-select";

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

@NgModule({
  imports:      [ BrowserModule, Ng2BootstrapModule, SELECT_DIRECTIVES],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

app.component.ts

@Component({
    selector: 'search',
    styleUrls: ['app/assets/ng2-select.css'],
    templateUrl: '<ng-select [items]="items" placeholder="Country">
                </ng-select>'
})
export class AppComponent {
    public items: Array<string> = ['Austria', 'Belgium', 'Bulgaria', 'Croatia',
    'Czech Republic', 'Denmark', 'England', 'Egypt', 'Finland', 'France',
    'Germany', 'Greece', 'Hungary', 'Ireland', 'Italy', 'Netherlands',
    'Poland', 'Spain'];

}

任何想法如何解决这个问题。

1 个答案:

答案 0 :(得分:3)

您必须在AppModule中导入SelectModule才能使其正常运行,并移除SELECT_DIRECTIVES

@NgModule({
  imports:      [ BrowserModule, Ng2BootstrapModule, SelectModule],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

请参阅npm package readme了解详情。