Search code examples
node.jsangulartypescript

The "export" keyword in the definition of Angular component class


I'm not expert about Angular so probably I'll ask something very trivial.

Useful info about my system (obtain by the command ng --version) are reported below:

Angular CLI: 13.2.0
Node: 16.13.2
Package Manager: npm 8.6.0
OS: linux x64

My question is about the code which defines a component. I suppose that the name of my component is events, so I execute the following command in a terminal:

ng generate component events

Previous command generates 4 files; one of these files is called events.component.ts.
Inside that, there is the declaration of the following class:

export class EventsComponent {
  ...
}

Why do I have to use the export keyword before the class keyword? What is it for?


Solution

  • This is not related to Angular, but to Typescript. If you want to use code (e.g. a class) in another .ts file, you have to first export it from your source file.

    If you don't need to import it like import {EventsComponent} from '.events.component' somewhere in your project, you also don't need to export it before. Nevertheless this is not the case for angular components, since they should be used anywhere else (e.g. in a module).