Home > OS >  Angular 12 how to get a value for environment value through a function
Angular 12 how to get a value for environment value through a function

Time:08-03

I have the need to do something like this:

export const environment = {
  production: true,
  serverUrl: this.setUrl()
};

so that on startup setUrl would be called and assign the returned value to serverUrl, how can I do this or something that basically does this without using anything other than the angular project itself?

CodePudding user response:

You need a few enviroment.ts configurations, like enviroment.prod.ts

So you can set different serverUrl for each.

On build you use a flag -configuration=prod

CodePudding user response:

done, here's what I did:

import { HttpClient, HttpXhrBackend } from "@angular/common/http";

export const environmentProd = {
  production: true,
  serverUrl: setUrl()
};

function setUrl() :string {
  let http = new HttpClient(new HttpXhrBackend({ 
    build: () => new XMLHttpRequest() 
  }));
  return "whatever"
}

now I'll use the httpClient to get a txt file from the app itself, I read it's content as a megaString and then I can play with it as I see fit. (evil laugh here)

  • Related