Angular switchMap不工作时角度获取路由参数

Angular switchMap不工作时角度获取路由参数,angular,routes,rxjs,Angular,Routes,Rxjs,我试图按照教程中关于如何在文档中获取管线参数的说明进行操作 我可以在使用subscribe时获取路由参数 然而,根据文档,使用switchMap是最佳实践。我无法让switchMap示例在我的代码中工作,我不知道为什么 import { switchMap } from 'rxjs/operators'; this.getRouteParams$ = this.route.params.pipe( switchMap(params => { // Does not work

我试图按照教程中关于如何在文档中获取管线参数的说明进行操作

我可以在使用subscribe时获取路由参数

然而,根据文档,使用switchMap是最佳实践。我无法让switchMap示例在我的代码中工作,我不知道为什么

import { switchMap } from 'rxjs/operators';

this.getRouteParams$ = this.route.params.pipe(
  switchMap(params => {
    // Does not work
    console.log(params);
    // How to check params here and then subscribe to other observables conditionally
  })
);
最终目标是使用switchMap在接收和验证路由参数后,根据路由参数的值有条件地调用其他可观测值。但是,我甚至无法使第一个switchMap语句如上图所示正常工作。

//这会奏效的 从“rxjs/operators”导入{switchMap}; this.getRouteParams$=this.route.params.pipe switchMapparams=>{ //不起作用 console.logparams; //如何在此处检查参数,然后有条件地订阅其他观察值 } .subscribeitem=>{ console.logitem } //这会奏效的 从“rxjs/operators”导入{switchMap}; this.getRouteParams$=this.route.params.pipe switchMapparams=>{ //不起作用 console.logparams; //如何在此处检查参数,然后有条件地订阅其他观察值 } .subscribeitem=>{ console.logitem
} 您需要订阅以评估Observable并根据param有条件地调用另一个Observable,请执行以下操作:

import { switchMap } from 'rxjs/operators';

this.getRouteParams$ = this.route.params.pipe(
  switchMap(params => {
    console.log(params);
    // How to check params here and then subscribe to other observables conditionally
    //here you can check the params and return the another observable like this:

    //BELOW code is just to show how to return another observable conditionally
    //Change it as per your logic
    const p = params['id']; //i am assuming that your param name is `id`; 
    if(p === 100) {
       return this.anotherObservable()
                  .pipe(
                     tap(r => {
                       //DO whaterver you want to do with the response of the observable
                       console.log(r);
                     })
                   );
    } else {
       return of(someThingAsPerYourLogic);
    }
  })
).subscribe();

您需要订阅以评估Observable并根据param有条件地调用另一个Observable,请执行以下操作:

import { switchMap } from 'rxjs/operators';

this.getRouteParams$ = this.route.params.pipe(
  switchMap(params => {
    console.log(params);
    // How to check params here and then subscribe to other observables conditionally
    //here you can check the params and return the another observable like this:

    //BELOW code is just to show how to return another observable conditionally
    //Change it as per your logic
    const p = params['id']; //i am assuming that your param name is `id`; 
    if(p === 100) {
       return this.anotherObservable()
                  .pipe(
                     tap(r => {
                       //DO whaterver you want to do with the response of the observable
                       console.log(r);
                     })
                   );
    } else {
       return of(someThingAsPerYourLogic);
    }
  })
).subscribe();

您仍然需要在某个位置订阅或通过管道异步,在您有另一个可观察对象要展开之前,使用switchMap没有任何意义。对于此场景,最好使用mergeMap。您仍然需要在某个位置订阅或通过管道异步,在有另一个可观察对象打开之前,使用switchMap没有任何意义。最好在此场景中使用mergeMap。订阅将让您看到最终结果,如果您想检查管道中的值,可以在参数中使用console.log,但所有逻辑都将进入管道,订阅可观察对象就是每次“执行”它的内容订阅将让您看到最终结果,如果您想检查管道中的值,可以在参数中使用console.log,但您的所有逻辑都将进入管道,订阅可观察对象就是“执行”它的内容