Angular 如何从一个可观察到的项目中使用另一个可观察到的项目

Angular 如何从一个可观察到的项目中使用另一个可观察到的项目,angular,rxjs,Angular,Rxjs,如何用路由器参数的结果替换123(我使用的是Angular 2) 通常,我会访问这样的参数 this.route.params .switchMap((params: Params) => this.customerService.getCustomer(+params['id'])) .subscribe((results: any) => this.data = results); 但是我不再有customerService,而是想使用apollo查询。但是不太确定如何

如何用路由器参数的结果替换
123
(我使用的是Angular 2)

通常,我会访问这样的参数

this.route.params
  .switchMap((params: Params) => this.customerService.getCustomer(+params['id']))
  .subscribe((results: any) => this.data = results);

但是我不再有customerService,而是想使用apollo查询。但是不太确定如何将它们放在一起?

您的
监视查询
不是被动的,因为您可以动态地替换传递给它的
变量。每当custNum发生更改并放弃以前的可观察项时,您需要设置一个新的
watchQuery
。您可以通过
.switch()
来完成此操作,当流发出新的可观察对象时,它将取消订阅以前的可观察对象

Rx.Observable.interval(500)
  .map(customerNumber => this.apollo.watchQuery({
    query: query,
    variables: {
      custNum: customerNumber
    }
  }) /* returns ObservableQuery */
  .switch() /* unsubscribes previous watchQuery, subscribes new one */
  .map(({data}) => data.allCustomers.nodes);

您的
watchQuery
不是被动的,因为您可以动态地替换传递给它的
变量。每当custNum发生更改并放弃以前的可观察项时,您需要设置一个新的
watchQuery
。您可以通过
.switch()
来完成此操作,当流发出新的可观察对象时,它将取消订阅以前的可观察对象

Rx.Observable.interval(500)
  .map(customerNumber => this.apollo.watchQuery({
    query: query,
    variables: {
      custNum: customerNumber
    }
  }) /* returns ObservableQuery */
  .switch() /* unsubscribes previous watchQuery, subscribes new one */
  .map(({data}) => data.allCustomers.nodes);

您不能在route.params的订阅中添加watchQuery吗?您不能在route.params的订阅中添加watchQuery吗?