Angular/RxJs 6-组合路由参数和查询参数不再工作

Angular/RxJs 6-组合路由参数和查询参数不再工作,angular,rxjs6,Angular,Rxjs6,我已经花了至少2个小时试图让第6版的东西工作,但没有用。我无法同时获得路由参数和查询参数 这是与旧版本最接近的语法,但它只记录查询参数 我想做的是将其包装在全局路由服务中,以便方法调用是干净的,并且如果发生任何其他更新,我可以在一个地方进行更改 import {BehaviorSubject, combineLatest, Observable} from 'rxjs'; constructor(private router: Router, private route: Activa

我已经花了至少2个小时试图让第6版的东西工作,但没有用。我无法同时获得路由参数和查询参数

这是与旧版本最接近的语法,但它只记录查询参数

我想做的是将其包装在全局路由服务中,以便方法调用是干净的,并且如果发生任何其他更新,我可以在一个地方进行更改

    import {BehaviorSubject, combineLatest, Observable} from 'rxjs';

constructor(private router: Router, private route: ActivatedRoute)
// body of constructor left out


     // Combine them both into a single observable
    const urlParams: Observable<any> = combineLatest(
        this.route.params,
        this.route.queryParams,
        (params, queryParams) => ({ ...params, ...queryParams})
    );

    urlParams.subscribe(x => console.log(x));
从'rxjs'导入{BehaviorSubject,combineLatest,Observable};
构造函数(专用路由器:路由器,专用路由:ActivatedRoute)
//遗漏了构造函数的主体
//将两者合并为一个可观察的对象
const urlParams:Observable=CombineTest(
this.route.params,
this.route.queryParams,
(params,queryParams)=>({…params,…queryParams})
);
subscribe(x=>console.log(x));
我还注意到,出于某种原因,combinedLatest不在“rxjs/operators”中。 Observable.CombineTest也不起作用


谢谢。

CombineTest以数组格式提供一个输出。。。 请尝试使用以下方法

t$ = combineLatest(
  this.route.params,
  this.route.queryParams
).pipe(
  map(results => ({params: results[0], queryParams: results[1]}))
);

对于rxjs6,没有更多的结果选择器,因此您需要使用“映射”。关于移徙的文件

从'rxjs'导入{BehaviorSubject,combineLatest,Observable};
从“rxjs/operators”导入{map}
const urlParams:Observable=CombineTest(
this.route.params,
this.route.queryParams
).烟斗(
映射(([params,queryParams])=>({…params,…queryParams}))
);
subscribe(x=>console.log(x));

我偶然发现了同样的问题,并且接受的答案很有效,但是如果同时更改路由参数和查询参数,订阅将被触发两次。 为了避免这种情况,我使用了
distinctUntilChanged

combineLatest(
      this.route.params.pipe(distinctUntilChanged(), takeUntil(this.ngUnsubscribe)),
      this.route.queryParams.pipe(distinctUntilChanged(), takeUntil(this.ngUnsubscribe))
    )
      .pipe(map(([params, queryParams]) => ({params, queryParams})))
      .subscribe(({params, queryParams}) => {
         console.log(params, queryParams);
      });

谢谢,但是当我尝试这个和console.log时,params总是空的。我意识到这在我的ngOnit组件中有效,但在我的singleton服务中无效。也就是说,route参数仅在组件内部使用时显示。不过,singleton服务确实显示了queryparam。我想知道将服务注入到组件而不是全局是否可以修复它。我建议在组件中使用它,因为在服务中使用时可能会有很多错误,因为有时它不会;我不知道激活路线。谢谢。我能够通过保持服务本地并返回可观察到的内容使其工作,尽管它同时发出当前和新的路由。我只是想避免在我的所有组件中直接调用路由器,因为Angular团队总是在更改这些API。我能包装的东西越多越好。@Smith我了解你:-)你不需要一个自定义比较器使你的区分更准确吗?
combineLatest(
      this.route.params.pipe(distinctUntilChanged(), takeUntil(this.ngUnsubscribe)),
      this.route.queryParams.pipe(distinctUntilChanged(), takeUntil(this.ngUnsubscribe))
    )
      .pipe(map(([params, queryParams]) => ({params, queryParams})))
      .subscribe(({params, queryParams}) => {
         console.log(params, queryParams);
      });