Angular 应用程序禁止查询参数

Angular application suppresses queryParams

我试图将各种可选参数放入路由的 queryParams 中,但由于某种原因,它们很快弹出并再次消失(正如我能够在 [=54 的地址栏中观察到的那样) =]).这是我的组件的设置:

ngOnInit() {
    this.paginator.page.subscribe(() => {
      console.log(`this.paginator.pageIndex: ${this.paginator.pageIndex}`);
      this.router.navigate([], {
        relativeTo: this.activatedRoute,
        queryParams: { page: this.paginator.pageIndex },
        queryParamsHandling: 'merge',
      });
    });

    this.activatedRoute.queryParams.subscribe((params) => {
      console.log(`params.page ${params.page}`);
    });

    this.router.events.subscribe((event) => {
      console.log(event);
    });
  }

在我的 VSCode 调试控制台中产生以下输出:

this.paginator.pageIndex: 2
src/app/tiles/consumption-report/consumption-report-view-events/consumption-report-view-events.component.ts:142
t {id: 4, url: '/consumptionReportManagement/viewEvents?page=2', navigationTrigger: 'imperative', restoredState: null}
t {id: 4, url: '/consumptionReportManagement/viewEvents?page=2', urlAfterRedirects: '/consumptionReportManagement/viewEvents?page=2', state: t}
t {id: 4, url: '/consumptionReportManagement/viewEvents?page=2', urlAfterRedirects: '/consumptionReportManagement/viewEvents?page=2', state: t}
t {id: 4, url: '/consumptionReportManagement/viewEvents?page=2', urlAfterRedirects: '/consumptionReportManagement/viewEvents?page=2', state: t, shouldActivate: true}
params.page 2
src/app/tiles/consumption-report/consumption-report-view-events/consumption-report-view-events.component.ts:138
e {snapshot: e}
e {snapshot: e}
e {snapshot: e}
e {snapshot: e}
e {routerEvent: t, position: null, anchor: null}
t {id: 5, url: '/consumptionReportManagement/viewEvents', navigationTrigger: 'imperative', restoredState: null}
t {id: 4, url: '/consumptionReportManagement/viewEvents?page=2', urlAfterRedirects: '/consumptionReportManagement/viewEvents?page=2'}
t {id: 5, url: '/consumptionReportManagement/viewEvents', urlAfterRedirects: '/consumptionReportManagement/viewEvents', state: t}
t {id: 5, url: '/consumptionReportManagement/viewEvents', urlAfterRedirects: '/consumptionReportManagement/viewEvents', state: t}
t {id: 5, url: '/consumptionReportManagement/viewEvents', urlAfterRedirects: '/consumptionReportManagement/viewEvents', state: t, shouldActivate: true}
params.page undefined
src/app/tiles/consumption-report/consumption-report-view-events/consumption-report-view-events.component.ts:138
e {snapshot: e}
e {snapshot: e}
e {snapshot: e}
e {snapshot: e}
e {routerEvent: t, position: null, anchor: null}
t {id: 5, url: '/consumptionReportManagement/viewEvents', urlAfterRedirects: '/consumptionReportManagement/viewEvents'}

如您所见,queryParams 被修改了两次,一次带有正确的参数,另一次没有参数。当我通过地址栏输入参数时,也会发生同样的事情。

我尝试了以下方法来解决这个问题

我正在开发一些组件作为更大应用程序的一部分,但我的 none 同事正在使用 queryParams,但没有人知道为什么应用程序会这样。

I checked every router.navigate or router.navigateByUrl project wide to see if there is some side effects

我又做了一遍,果然发现了问题所在。就这样解决了。