Angular 是否可以检测查询参数是否已更改?

Angular is it possible to detect if query Paramter has changed?

Angular 是否可以检测查询参数是否已更改? 我有一个处理 2 个查询参数的组件,并根据您附带的参数将一些变量设置为 true 或 false。问题是,如果我在我的组件上使用查询参数 1,那么 qp 可以在不离开组件的情况下更改为第二个参数,在这种情况下,我需要不同地设置变量。那么我该如何检测呢?这可能吗?

您可以在根组件中订阅参数

constructor(route:ActivatedRoute) {
    route.queryParams.subscribe(p => console.log(p.myQueryParam)); // you can also do this in ngOnInit
}

另见 https://angular.io/api/router/ActivatedRoute

您也可以在其他路由级别上使用查询参数,但它们被称为矩阵参数。

另见本节结尾https://angular.io/guide/router#route-parameters-required-or-optional

您可以订阅ActivatedRoute模块提供的params可观察对象,观察查询参数的变化

constructor(private route:ActivatedRoute) {
    this.route.params.subscribe(
        (params:Params)=>{
            console.log(params['yourId']);
    };
)}