我有一个简单的模块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 { }
和应用模块:
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 },
]
)]
在计数器组件上找不到 TS 错误
在AppModule
中,从模块属性中删除CounterComponent
declarations
。他已经在SomeOtherModule
上宣布了.
编辑:我不知道你是否为这个问题削减了它,但如果没有,你忘了在你的AppModule
文件中导入CounterComponent
。