最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Angular 19 - Component AppComponent is standalone, and cannot be declared in an NgModule - Stack Overflow

programmeradmin1浏览0评论

On my Angular v19 app I get the following error:

Component AppComponent is standalone, and cannot be declared in an NgModule

or

'imports' is only valid on a component that is standalone.

It worked perfectly in v18 before the update.

What happened?

On my Angular v19 app I get the following error:

Component AppComponent is standalone, and cannot be declared in an NgModule

or

'imports' is only valid on a component that is standalone.

It worked perfectly in v18 before the update.

What happened?

Share Improve this question edited Nov 19, 2024 at 20:22 Matthieu Riegler asked Nov 18, 2024 at 10:35 Matthieu RieglerMatthieu Riegler 57.1k27 gold badges149 silver badges200 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 13

In v19, Angular swapped the default value for standalone from false to true. It means that every component declared is now implicitly standalone.

If your app relies on NgModule that declare components/pipes/directives, you will need to add standalone: false to all of those:

@Component({
  standalone: false,  // this is now required when using NgModule
  ...
})
export class AppComponent {}

@NgModule({
  declaration: [AppComponent], 
  ...
})
export class AppModule {}

Also, as part of the migration/update experience, the Angular team provides a migration schematic to update your apps and take care of most of the breaking changes. For this you only need to run ng update.

Make sure to also have a look at the update guide.

Make the following changes in appponent.ts file.

  1. standalone: false
  2. Remove imports

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论