我已经设置了以下routing system

export const MyRoutes: Routes = [
  {path: '', redirectTo: 'new', pathMatch: 'full'},
  {path: ':type', component: MyComponent}
];

有以下navigation system

goToPage('new');
goToPageNo('new', 2);

goToPage(type) {
  this.router.navigate([type]);
}
goToPageNo(type, pageNo) {
  this.router.navigate([type], {queryParams: {page: pageNo}});
}

示例URL如下所示

http://localhost:3000/new

http://localhost:3000/new?page=2

http://localhost:3000/updated

http://localhost:3000/updated?page=5

Sometimes they have 100 queryParams

现在我需要读这两本书

ngOnInit(): void {
  this.paramsSubscription = this.route.params.subscribe((param: any) => {
    this.type = param['type'];
    this.querySubscription = this.route.queryParams.subscribe((queryParam: any) => {
      this.page = queryParam['page'];
      if (this.page)
        this.goToPageNo(this.type, this.page);
      else
        this.goToPage(this.type);
    })
  })
}

ngOnDestroy(): void {
  this.paramsSubscription.unsubscribe();
  this.querySubscription.unsubscribe();
}

现在,这并没有像预期的那样起作用,访问没有queryParams的页面是可行的,然后我访问带有queryParams的页面"goToPageNo"会被多次调用,因为我在route params中订阅queryParams.

我看过ANGLING 2文档,它们没有任何同时实现both route params and queryParams订阅的示例或代码.

有什么方法可以做到这一点吗?有什么建议吗?

推荐答案

对于Angular 6+

import { combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';

...

combineLatest(this.route.params, this.route.queryParams)
    .pipe(map(results => ({params: results[0].xxx, query: results[1]})))
    .subscribe(results => {
        console.log(results);
    });

从您的路由出发的xxx号航班在哪里?

{path: 'post/:xxx', component: MyComponent},

Angular相关问答推荐

Angular 信号:编程信号量

使用多个管道时Angel 17空数据

文件阅读器未正确给出某些CSV文件的结果

以Angular 将指令事件从子组件传递到父组件

MAT表的内联文本编辑-未刷新编辑图标

TransLoco合并本地和服务器端转换对象

是否可以将Angular 路由配置为在点击带有本地人力资源S的锚点时更新Angular 路由?

如何确保[共享]组件是真正的哑组件?(Angular )

无法匹配任何路由.URL段:';英雄';

Angular NG5002错误无效的ICU消息和意外字符EOF

JsPDF Autotable 将图像添加到列中出现错误:属性getElementsByTagName不存在

如何处理Angular 延迟订阅

angular material日期 Select 器中的日期不正确

如何以Angular 2 获取当前路由自定义数据?

visibility:hidden

Angular:ViewChild 上未定义 nativeElement

AOT - Angular 6 - 指令 SomeComponent,预期 0 个参数,但go 有 1 个参数

样式 html,来自 Web 组件的正文

如何在Angular中使用带有 td 属性 colspan 的属性绑定?

如何在 Angular CLI 6: angular.json 中添加 Sass 编译?