0

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?

4

2 回答 2

0

你为什么不把这些放在environment.ts里面?您还可以为 等保留不同的prod环境dev

export const environment = {
  production: false,
  envName: 'local',
  baseUrl: '',
  proxyUrl: '',
  servicesUrl: ''
};

在您的服务中:

import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class MyService {

  constructor(private http: HttpClient) { }

  getData() {
    return this.http.get(`${environment.server}/remaining/path`)
  }

}
于 2018-03-20T14:42:06.907 回答
0

本身没有解决。我现在这样使用它:

@Injectable()
export class Globals {
  /** API URLS */
  public baseURL:string = '';
  public loginURL:string = 'rest/login';
  public proxyURL:string = 'rest/proxy';
  public servicesURL:string = 'rest/serviceRegistry';
}
于 2018-05-04T06:54:57.677 回答