角度是否可以检测查询参数是否已更改? [英] Angular is it possible to detect if query Paramter has changed?
问题描述
角度是否可以检测查询参数是否已更改? 我有一个处理2个查询参数的组件,根据您所附带的参数,它设置了一些变量true或false.问题是,如果im在我的带有queryparameter 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-可选
Angular is it possible to detect if query Paramter has changed? I have a Component that handles 2 query paramters and according to what paramter you come with it sets some variables true or false. The Problem is if im on my component with queryparameter 1 the qp can change to the second paramter without leaving the component and in that case i need to set variables diffrently. So how do I detect that? Is that even possible?
You can subscribe to the params in the root component
constructor(route:ActivatedRoute) {
route.queryParams.subscribe(p => console.log(p.myQueryParam)); // you can also do this in ngOnInit
}
See also https://angular.io/api/router/ActivatedRoute
You can have query params on other route levels as well, but then they are called matrix parameters.
See also the end of this section https://angular.io/guide/router#route-parameters-required-or-optional
这篇关于角度是否可以检测查询参数是否已更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!