6

Does anyone know if it’s possible to export enums in Angular modules? If not, are there any best practises to ship enums within Angular modules?

// not working example
// i dont know how to export GreatEnum

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { GreatComponent } from './great.component';
import { GreatEnum } from './great.enum';

@NgModule({
    imports: [
        CommonModule       
    ],
    declarations: [GreatComponent ],
    exports: [GreatComponent ]
})
export class GreatModule {
}
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Christoph Glaß
  • 149
  • 1
  • 1
  • 12

2 Answers2

8

If you are writing libraries, you have to export enums with the keyword const

export const enum <ENUM_NAME>
Amine Safi
  • 255
  • 4
  • 8
7

Why you need to export enum from the modules?. It is not necessary . It is like an interfaces and classes. You can use it everywhere, except directly in the templates.

You can just import them in any file which you want and use there. For them there is no error like

Directive or Component is not found

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
  • 7
    I'm writing modules for reusing them in several angular apps. Thats why I want to ship enums within the module. – Christoph Glaß Sep 26 '17 at 06:54
  • 2
    Look. When you will add your modules in the packages of an application, you can export it from the `barrel` file of your package. It may contains for example `export yourModule from yourFile; export yourEnum from yourEnumFile` and then get your module and enum like `import { YourModule, YourEnum } from YourPackage;` – Suren Srapyan Sep 26 '17 at 06:56