Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2 APP_初始值设定项嵌套http请求_Angular_Angular Cli - Fatal编程技术网

Angular2 APP_初始值设定项嵌套http请求

Angular2 APP_初始值设定项嵌套http请求,angular,angular-cli,Angular,Angular Cli,我一直在尝试在引导过程中使用APP\u初始值设定项,以加载一些配置数据(类似于、和其他) 我面临的问题是,我需要发出2个请求,第一个请求指向URL所在的本地json文件,然后请求指向该URL以获取实际配置 然而,由于某种原因,启动不会延迟,直到承诺解决为止 这是通过APP\u初始值设定项调用的加载方法 public load():演示问题(检查控制台输出) 很明显,我对这个概念缺少一个重要的理解 如何让应用程序在加载组件之前等待两个请求返回? 您没有在提供的PRUNKR中返回任何承诺: 导出函数

我一直在尝试在引导过程中使用
APP\u初始值设定项
,以加载一些配置数据(类似于、和其他)

我面临的问题是,我需要发出2个请求,第一个请求指向URL所在的本地json文件,然后请求指向该URL以获取实际配置

然而,由于某种原因,启动不会延迟,直到承诺解决为止

这是通过
APP\u初始值设定项调用的加载方法

public load():演示问题(检查控制台输出)

很明显,我对这个概念缺少一个重要的理解

如何让应用程序在加载组件之前等待两个请求返回?

您没有在提供的PRUNKR中返回任何承诺:

导出函数初始化(配置:ConfigService){ return()=>{ config.load(); }; }
实际上应该是:

导出函数初始化(配置:ConfigService){ return()=>config.load(); }

导出函数初始化(配置:ConfigService){ return()=>{ 返回config.load(); } } 1)退货承诺

export function init(config: ConfigService) {
  return () => config.load();
}
2) 维持秩序

public load(): Promise<any> {
  return this.http.get('./src/config.json')
        .map((res) => res.json())
        .switchMap(config => {
          return this.http.get('./src/' + config['URL']).map(r => r.json());
        }).toPromise().then((r) => {
          this.config = r;
        });
}
public load():Promise

或者使用我们的rxjs操作符

public load(): Promise<any> {
  return new Promise(resolve => {
    const promise = this.http.get('./src/config.json').map((res) => res.json())
      .subscribe(config => {
        let url = config['URL'];
        this.http.get('./src/' + url).map(r => r.json())
          .subscribe(r => { 
            this.config = r;
            resolve();
          });
      });
  });
}

public load():Promise

对我来说,
switchMap()
是缺少的链接,非常感谢!你是对的,这是从一个早期的尝试,我忘了把它改回来,谢谢你抓住它!