Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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
Angular 启动时角度服务和请求依赖关系_Angular_Angular Cli_Angular Services - Fatal编程技术网

Angular 启动时角度服务和请求依赖关系

Angular 启动时角度服务和请求依赖关系,angular,angular-cli,angular-services,Angular,Angular Cli,Angular Services,我在angular 4中工作,我有一个appConfigService,它从settings.json文件提供api应用程序的url,类似于: private appConfig: AppConfig; constructor(private http: Http) { this.loadConfig().subscribe(e => this.appConfig = e); } public loadConfig(): Observable<AppConfig> {

我在angular 4中工作,我有一个appConfigService,它从settings.json文件提供api应用程序的url,类似于:

private appConfig: AppConfig;

constructor(private http: Http) {
    this.loadConfig().subscribe(e => this.appConfig = e);
}
public loadConfig(): Observable<AppConfig> {
    return this.http
      .get("appsettings.json")
      .map(this.mapAppConfig)
      .catch(this.handleError);
}
private-appConfig:appConfig;
构造函数(专用http:http){
this.loadConfig().subscribe(e=>this.appConfig=e);
}
public loadConfig():可观察{
返回此文件。http
.get(“appsettings.json”)
.map(this.mapAppConfig)
.接住(这个.把手错误);
}
然后,我将该url用于以下所有请求

对于url返回后发送的请求,它可以正常工作,但是我在页面加载时发送的请求有问题(并且url还没有被检索到)

我知道我可以在启动时加载的服务中使用flatmap函数,但我发现自己基本上创建了相同函数的两个实现:

private getBasicRequest(offset: number = 0, limit:number = 0 ): Observable<Response>{
    if (!this.appConfigService.isAppConfigValid()) {
      var urlObserver = this.appConfigService.loadConfig();

      var request = urlObserver
        .flatMap(responseFromConfigService =>
          this.http.get(responseFromConfigService.apiBaseAddress + "/entries/" + this.searchTerm + "?offset=" + offset + "&limit=" + limit));

      return request;

    } else {
      var url = this.appConfigService.getApiLink("entries/" + this.searchTerm + "?offset=" + offset + "&limit=" + limit);
      return this.http.get(url);
    }
  }
private getBasicRequest(偏移量:number=0,限制:number=0):可观察{
如果(!this.appConfigService.isAppConfigValid()){
var urlObserver=this.appConfigService.loadConfig();
var请求=urlObserver
.flatMap(responseFromConfigService=>
this.http.get(responseFromConfigService.apiBaseAddress+”/entries/“+this.searchTerm+”?offset=“+offset+”&limit=“+limit”);
返回请求;
}否则{
var url=this.appConfigService.getApiLink(“条目/”+this.searchTerm+”?offset=“+offset+”&limit=“+limit”);
返回此.http.get(url);
}
}
处理这种情况的更好方法是什么?
我试图让appConfigService服务在“启动”时启动,这将解决我的问题,但没有成功。

始终使用observable,而不是订阅它们。您的配置服务应该如下所示:

export class AppConfigService
{
    public config$: Observable<AppConfig>;

    constructor(private http: Http) {
      this.config$ = this.http
          .get("appsettings.json")
          .map(this.mapAppConfig)
          .catch(this.handleError)
          .share();
    }
}
private getBasicRequest(offset = 0, limit = 0): Observable<Response> {
      var config$ = this.appConfigService.config$;

      var request = config$
        .map(config => config.apiBaseAddress)
        .map(base => `${base}/entries/${this.searchTerm}?offset=${offset}&limit=${limit})
        .flatMap(url => this.http.get(url));

      return request;
  }
导出类AppConfigService
{
公共配置$:可观察;
构造函数(专用http:http){
this.config$=this.http
.get(“appsettings.json”)
.map(this.mapAppConfig)
.接住(这个.把手错误)
.share();
}
}
所有其他服务都是这样使用它的:

export class AppConfigService
{
    public config$: Observable<AppConfig>;

    constructor(private http: Http) {
      this.config$ = this.http
          .get("appsettings.json")
          .map(this.mapAppConfig)
          .catch(this.handleError)
          .share();
    }
}
private getBasicRequest(offset = 0, limit = 0): Observable<Response> {
      var config$ = this.appConfigService.config$;

      var request = config$
        .map(config => config.apiBaseAddress)
        .map(base => `${base}/entries/${this.searchTerm}?offset=${offset}&limit=${limit})
        .flatMap(url => this.http.get(url));

      return request;
  }
private getBasicRequest(偏移量=0,极限=0):可观察{
var config$=this.appConfigService.config$;
var请求=config$
.map(config=>config.apiBaseAddress)
.map(base=>`${base}/entries/${this.searchTerm}?offset=${offset}&limit=${limit})
.flatMap(url=>this.http.get(url));
返回请求;
}

您可以使用或感谢您的回答!我尝试了这种方法,但问题是我在每次请求时都会请求appsettings.json,我只想请求一次,它只包含一个url,而这个url在应用程序中随处可见。谢谢,这很有效,但是有没有办法避免在所有其他请求中使用flatmap?在appsettings.json中,我有基本的api url,因此我在任何地方都使用它,在任何地方使用flatmap都有点烦人。