服务Angular 5退货后使用诺言

时间:2018-07-31 09:47:46

标签: angular http angular-promise

在Angular应用程序中,我在资产文件夹中创建了一个“ config.json”,在其中存储了一些有关App的设置以供进一步使用。

config.json中的

json数据为

{
    "config":{
    "worker_url":"http//:domainname/abc"   
    }
}

从组件中,我正在调用服务方法以从config.json获取数据。

 this.app_config = this._configService.getConfigResponse().then( (res) => {
    return res;
  });
 // app_config is Object;
   console.log(this.app_config);

我在Service中有一个名为“ ConfigServices”的功能

getConfigResponse() {
  let promise = new Promise((resolve, reject) => {
    let apiURL = this._configFilePath;
    this.http.get(apiURL)
      .toPromise()
      .then(
        res => { // Success
         this.results = res.json().config;  
      //   console.log(res.json().config);
          resolve(this.results);
        },
        msg => { // Error
        reject(msg);
        }
      );
  });
  return promise;
}

控制台日志中的结果为:

enter image description here

不了解如何获取此json数组以进一步使用它。

1 个答案:

答案 0 :(得分:0)

我想您的问题是这些行:

SYS."_user_stat"

尝试将其重写为

this.app_config = this._configService.getConfigResponse().then( (res) => {
    return res;
 });

如果可以使用async / await,则可以将代码重写为:

this._configService.getConfigResponse().then( (res) => {
    this.app_config = res;
});