使用一个模块的组件作为app模块路径中的路径

时间:2018-05-22 07:54:50

标签: angular

我有简单的模块SomeOtherModule,我希望它的 CounterComponent 可用作AppModule中的路径。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CounterService } from '../services/counter.service';
import { CounterComponent } from '../counter/counter.component';
@NgModule({  
  imports: [ CommonModule],
  providers: [CounterService],
  declarations: [  CounterComponent ],
  exports: [CounterComponent] 
})
export class SomeOtherModule { }

和AppModule:

import { SomeOtherModule } from './some-other/some-other.module';
@NgModule({
    declarations: [
       AppComponent,  
       HomeComponent,     
       CounterComponent, 
    ],
    imports: [SomeOtherModule,
         RouterModule.forRoot([
          { path: '', component: HomeComponent, pathMatch: 'full' },
          { path: 'counter', component: CounterComponent }, 
     ]
)]

未找到CounterComponent时出现TS错误

1 个答案:

答案 0 :(得分:1)

AppModule中,从CounterComponent模块属性中删除declarations。他已经在SomeOtherModule宣布了。

编辑:我不知道您是否针对该问题进行了剪切,但如果没有,则忘记在CounterComponent文件中导入AppModule