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

On my Angular v19 app I get the following error:Component AppComponent is standalone, and cannot be de

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745625841a4636800.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信