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

Kai*_*Kai 12 query-parameters angular

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

Gün*_*uer 18

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

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

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

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

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