Search code examples
angularangular5injectable

Angular 5: Run Constructor before beeing injected


I have a file with global variables:

@Injectable()
export class Globals {
  public baseURL:string;
  public loginURL:string;
  public proxyURL:string;
  public servicesURL:string;

  constructor(platformLocation: PlatformLocation) {
    this.baseURL = (platformLocation as any).location.href;
    this.loginURL = this.baseURL + 'rest/login';
    this.proxyURL = this.baseURL + 'rest/proxy';
    this.servicesURL = this.baseURL + 'rest/serviceRegistry';
  }
}

At the moment my API-Calls fail because the variables aren't set yet. Is there a way to only inject this service when the constructor is run or do I have to use Observables?


Solution

  • Didnt solve it per se. I'm using it like this now:

    @Injectable()
    export class Globals {
      /** API URLS */
      public baseURL:string = '';
      public loginURL:string = 'rest/login';
      public proxyURL:string = 'rest/proxy';
      public servicesURL:string = 'rest/serviceRegistry';
    }