How I can use variable f…

1 minute read

How I can use variable from environment in shared lib in case of multiple angular apps ? For example ```app/admin/src/environments/environment.ts (contain API_KEY: xxx) app/user/src/environments/environment.ts (contain API_KEY: xxx)

in libs/shared-service/…/api.service.ts import { environment } from “???/environment”; apiKey = environment.API_KEY```

Responses:

Create your own injection token as shared lib and then you prefill the token on on application level

So an environment injection token

This one you can you everywhere

Can you show an example on Github or someelse ?

Let me search ;)

We do exactly what is explaining! Created an environment injection token and provide it in the app module. That is replaced at build time with the build configuration file replacement.

I can show an example tomorrow if you need it!

thanks, it will be great

Are you mean define variable in shared libs and implement it in environment.ts in particular app?

You define the token and the interface in the shared lib but values are set in the application

make sense, you give me some idea. thanks.

Can’t find a good example so quick ;)

Here is someone writing about it to get the idea ;)

I will still need you example tomorrow :)

<https://link.medium.com/0ZleHVrmy6 https://link.medium.com/0ZleHVrmy6>

Sounds good! I’ll see if I can whip something up!

how we did it was ``````

first declare export const ENVIRONMENT_VARIABLE = new InjectionToken&lt;EnvironmentVariable&gt;('ENVIRONMENT_VARIABLE')

in your lib/package

and then in your app, do something like this { provide: ENVIRONMENT_VARIABLE, useFactory: () =&gt; new Environment() }

you can either useFactory or useClass

that goes in your module’s provider in your apps … it can be either in your core.module app.module or any specific module if the lib is only used there

hope it helps, cheers :smile:

Updated: