mercredi 4 décembre 2019

How to use result from Angular 6 subscribe() method outside?

Hello I am trying to create a config.json file with just a few urls and read the data in a Service.

.../.../assets/config.json

{
    "registration" : "localhost:4200/registration"
    "login" : "localhost:4200/login"
}

../services/config.service.ts

export class ConfigService {
  result;
  configUrl = '../../assets/config.json';

  constructor(private http: HttpClient) {}

  getConfig() {
    return this.http.get(this.configUrl).subscribe((data) => { this.result = data });
  }
}

In the specific login.service.ts and registration.service.ts I call the getConfig() to handle the specific urls. The problem is that in this services the return value is undefined but I need the result out of the subscribe/getConfig method.

Now I am watching about 3 hours for a solution but I do get much more confused as more as I read so I would like to ask for help.

I saw solutions with .map() method (but I guess this method does not exist anymore), with "Promises", with export interface but nothing worked.

example for ../registration.service.ts

export class RegistrationService {

  private api_url;

  constructor(private http: HttpClientModule, private cs: ConfigService) {
    this.api_url = this.cs.getConfig();
  }
}



Aucun commentaire:

Enregistrer un commentaire