2017-06-01 89 views
0

我创建了一个要由多个其他组件共享的组件。我将通用组件声明为模块A,它工作正常。我在模块B中做了同样的事情,我得到了这个错误。Angular 2共享组件错误

类型PagerComponent是2个模块声明的一部分:ModuleA和ModuleB!请考虑将PagerComponent移动到导入ModuleA和ModuleB的较高模块。您还可以创建一个新的NgModule,它导出并包含PagerComponent,然后在ModuleA和ModuleB中导入该NgModule。

那么我创建了一个寻呼模块(NgModule)。

import { NgModule } from '@angular/core'; 
import { PagerComponent } from '../shared/pager.component'; 

@NgModule({ 
    imports: [ 
     PagerComponent 
    ] 
}) 
export class PagerModule {} 

我再导入这个新模块ModuleA和模块B.我得到错误:

Uncaught Error: Unexpected value 'undefined' imported by the module 'PagerModule' 

此错误是不会告诉我什么是错。有任何想法吗?

回答

1

我发现您的NgModule声明存在问题。 感谢NgModule结构,您不能将除NgModule之外的任何内容传递给imports字段。在你的情况下,你需要通过PagerComponentdeclarationsexports模块的字段。

declarations你很明显地声明你的组件,管道和指令。此外,您还需要将它们传递到exports部分,以使其对目标模块中的其他组件“可见”

例如,

import { NgModule } from '@angular/core'; 
import { CommonModule } from '@angular/common'; 
import { PagerComponent } from '../shared/pager.component'; 

@NgModule({ 
    imports: [ CommonModule ], 
    declarations: [ PagerComponent ], 
    exports: [ PagerComponent ] 
}) 
export class PagerModule {}