Component imports must be standalone components, directives, pipes, or must be NgModules while importing an NgModule
Asked Answered
G

2

1

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 {}
Gable answered 8/1, 2023 at 11:57 Comment(0)
G
2

I lost over 30 minutes, on this.

The NgModule is malformed : NgModule should have been @NgModule.

Gable answered 8/1, 2023 at 11:57 Comment(0)
W
0

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 {
  ...
}
Withal answered 23/7 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.