4

I want to make a library to use in my Angular 4 applications, and for some business I will do on it I have an enum that the applications will need to use later. Problem is, I declare the enum like this in my-enum.ts:

export enum MyEnum {
    value1 = 1,
    value2 = 2,
    value3 = 3
}

But then, I can't seem to import it from my app when I do the following:

import { MyEnum } from 'my-library';

How should I proceed to have this correctly?

7
  • Is your library stored in node_modules? Commented Aug 31, 2017 at 22:09
  • Yes, that would be the idea Commented Aug 31, 2017 at 22:09
  • Do you have an index.ts file in the root location of the library? Commented Aug 31, 2017 at 22:14
  • Yes. It's content is export * from './public_api'; Commented Aug 31, 2017 at 22:16
  • 1
    you have to export also the enum as well or you will have to add the relative path to the enum in the import statement like import { MyEnum } from '../node_modules/my-library/myenum'; Commented Aug 31, 2017 at 22:18

2 Answers 2

7

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

export const enum <ENUM_NAME>
Sign up to request clarification or add additional context in comments.

Comments

-3

I had the same issue, but I simply forgot to build my library before trying to use the exported enum (ng build --prod my-library).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.