gpt4 book ai didi

Angular 错误 : Please add a @NgModule annotation

转载 作者:太空狗 更新时间:2023-10-29 17:12:47 27 4
gpt4 key购买 nike

我是 Angular 的新手,我正在尝试编写一个简单的模块化应用程序。但是我收到以下错误:

Uncaught Error: Unexpected directive 'SomeComponent' imported by the module 'SomeModule'. Please add a @NgModule annotation.

基本上,我不想导入app.module.ts中的所有服务和组件,我想模块化代码但我失败了。

这是app.module.ts

@NgModule({
declarations: [
AppComponent,
HomeComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(appRoutes),
HttpClientModule,
ReactiveFormsModule,
SomeModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}

这是嵌套模块:

@NgModule({
imports: [
CommonModule,
SomeComponent
],
providers: [SomeService],
declarations: [],
exports: [SomeComponent]
})
export class SomeModule {
}

这是嵌套组件:

@Component({
selector: 'app-index',
templateUrl: './some.component.html',
styleUrls: ['./some.component.css']
})
export class SomeComponent implements OnInit {

somes: Array<ISome>;

constructor(private http: HttpClient, private service: SomeService) {}

ngOnInit() {
this.getSomes();
}

getSomes() {
this.service.getSomes().subscribe(res => {
this.somes = res;
});
}
}

最佳答案

您正在尝试“导入”SomeModule 中的组件。

imports: [
CommonModule,
SomeComponent
],

导入模块并声明组件,这正是错误消息告诉您的内容——您尝试导入指令SomeComponent

Unexpected directive 'SomeComponent' imported by the module 'SomeModule'.

SomeComponentimports 移到 declarations

imports: [
CommonModule,
],
providers: [SomeService],
declarations: [
SomeComponent,
],

关于 Angular 错误 : Please add a @NgModule annotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52417373/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com