如何过滤 activatedRoute.queryParams 并根据过滤器订阅不同的 observable

how to filter activatedRoute.queryParams and subscribe to different observables according to the filter

问题:我的应用程序可能会收到查询字符串参数,根据查询字符串中的参数,需要执行不同的操作:

1) 在这里,我在查询字符串中寻找 advertiser,如果它在那里,我需要订阅一个将验证广告商的服务:

this.activatedRoute.queryParams
  .filter(data => data.hasOwnProperty('advertiser'))
  .mergeMap(v => {
    advertiserId = v.advertiser;
    return this.myService.advertiserCheck(v.advertiser);
  })
  .subscribe(data => {
    if (true === data.valid) {
      // some processing takes place
    }
  });

2) 在这里我寻找 orderKey param:

this.activatedRoute.queryParams
  .filter(data => data.hasOwnProperty('orderKey'))
  .mergeMap(() => this.activatedRoute.firstChild.firstChild.url)
  .subscribe(segments => {
    // some processing takes place with the url segments
  });

如您所见,我订阅了 ActivatedRoute.queryParams 两次。有什么办法可以将两者结合起来吗?我尝试了以下方法:

this.activatedRoute.queryParams
  .filter(data => data.hasOwnProperty('advertiser') || data.hasOwnProperty('orderKey'))
  .mergeMap(v => {
    if (v.hasOwnProperty('advertiser')) {
      advertiserId = v.advertiser;
      return this.myService.advertiserCheck(v.advertiser);
    } else {
      return this.activatedRoute.firstChild.firstChild.url;
    }
  })
  .subscribe(data => {
    console.log('which one am I handling?');
});

这些参数不会同时出现在查询字符串中,但在订阅中我不知道我在处理哪一个。我想我可以创建一个变量,然后在订阅中检查它,但它看起来不是很优雅。有没有更好的办法?我应该保留原样以便减少混淆吗?

我会将它们保留为 2 个独立的序列,因为它们有不同的最终结果。但我也会让它们与 shareReplay 运算符共享相同的底层可观察序列。

ngOnInit(){
    this.obs$ = this.activatedRoute.queryParams
        .shareReplay();

    // sequence 1
    this.obs$
        .filter(data => data.hasOwnProperty('advertiser'))
        .mergeMap(v => {
            advertiserId = v.advertiser;
            return this.myService.advertiserCheck(v.advertiser);
        })
        .subscribe(data => {//process});

    // sequence 2
    this.obs$
        .filter(data => data.hasOwnProperty('orderKey'))
        .mergeMap(() => this.activatedRoute.firstChild.firstChild.url)
        .subscribe(segments => {//process});
}