English 中文(简体)
构成部分进口必须是独立的部件、指令、管道或进口 NgModule 时必须是 NgModule
原标题:Component imports must be standalone components, directives, pipes, or must be NgModules while importing an NgModule
  • 时间:2023-01-08 11:57:01
  •  标签:
  • angular
I m trying to import an NgModule into a standalone component but the compiler is throwing the error NG2012 : Component imports must be standalone components, directives, pipes, or must be NgModules. NgModule({ declarations: [FooComponent], exports: [FooComponent], }); export class FooModule {} let comps = [ImageComponent, HighlightDirective]; @Component({ selector: app-root , standalone: true, imports: [FooModule], template: ``, }) export class ExampleStandaloneComponent {}
最佳回答
I lost over 30 minutes, on this. The NgModule is malformed : NgModule should have been @NgModule.
问题回答
I ran into this error message while prototyping something and just quickly added a @Directive to the same file as my component: // ❌ Component imports must be standalone components, // directives, pipes, or must be NgModules.(-992012) @Directive({ selector: [myDirective] , standalone: true, }) class MyDirective { ... } @Component({ standalone: true, imports: [ MyDirective ], }) export class MyComponent { ... } Exporting the directive class resolved the issue: // ✅ Works without error! @Directive({ selector: [myDirective] , standalone: true, }) export class MyDirective { // <---- added "export" ... } @Component({ standalone: true, imports: [ MyDirective ], }) export class MyComponent { ... }




相关问题
Angular matSort not working on Date column by desc

Trying to sort the material table with date column , date format is MM/DD/YYYY ,h:mm A , order of date is not by latest date and time. Anything which i missed from the below stackblitz code. https:/...

热门标签