0

I've created an angular library, say library1, which defines and exposes a service APIService. The APIService has a constructor argument (URL: string), which is injected with an Injection token defined as,

const URL_TOKEN = new InjectionToken<string>('API_BASE_URL');

I use this library1 in yet another library, say library2, i.e. I inject the APIService within a component called LoginComponent.

Now I have a full-fledged angular application, and I use the LoginComponent from library2, and I wish to provide the value of URL from the application.

I wonder if there is a way to achieve this. I'm also not sure how to expose URL_TOKEN from library1 all the way to the angular application in a nice way.

Update

When I try to inject API_BASE_URL as,

providers: [ {provide: new InjectionToken<string>('API_BASE_URL'), useValue: 'http://endpoint'} ]

I get an exception in the browser: ERROR NullInjectorError: StaticInjectorError(AppModule)[InjectionToken API_BASE_URL]:

2
  • take a look at this answer it might solve your problem Commented Jul 3, 2019 at 6:14
  • Thanks @ysf, I checked it. Anyway ended up exporting URL_TOKEN. Commented Jul 4, 2019 at 15:58

1 Answer 1

0

I ended up exporting URL_TOKEN all the way. After going through bunch of resources (stack overflow questions, github posts, angular docs etc.), that seems to be the right way to do it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.