Passing environment variables to custom library with forRoot()

1.7k Views Asked by At

I am trying to pass environment specific variables to my own library. And I'm passing it through forRoot(). For hard-coded values i am able to pass it but for environment values when i pass it to the library, it shows as undefined.

const rootUrl = environment.rootURL;

@NgModule({
imports: [
// configs go in here
coachModule.forRoot(
  {
    apiURL: 'test.com',
    scoreData: {
      score: 708
    },
    rootURL: rootUrl
  }),
 ...

rootUrl is defined when I put a break-point on @NgModule however when i put a break-point in the library rootURL is undefined.

I am using Angular 5. I have tried googling the problem but I couldn't find any solutions.

Is this a common problem and what could be another way of passing this environment variable? Any help appreciated :)

UPDATE: I found out why it wasnt working properly. It was building with --aot so when i debugged, I was able to see the value of rootUrl because i was checking it on the runtime but it was passing it to my library on the compile time (with undefined value)

I am trying this possible solution right now. If anyone has any other potential solutions, I would appreciate

SOLUTION: My solution was to pass the variables as functions since environment variables were compiled aot. This was it would have to execute them on the runtime.

I just created the functions like following and passed the reference to the library

export function getRootURL() {
   return environment.rootURL;
}
0

There are 0 best solutions below