Angular:检查所有路由的查询参数的最佳方法

huangapple go评论138阅读模式
英文:

Angular : Best way to check queries parameters for all routes

问题

我想检查所有路由的所有查询参数。
我不想使用守卫,因为检查可能会很长。
我想在导航后检查,例如在AfterViewInit中。
如何最好地检查所有路由的所有查询参数?

谢谢。

英文:

I would like to check all queries parameters for all route.
I dont want to use the guard because the checking may be long.
I would like to check after navigation for example in AfterViewInit.
What is the best way to check all queries parameters for all route ?

Regards

答案1

得分: 1

Sure, here's the translated code:

尝试 ActivatedRoute:

constructor(private route: ActivatedRoute) {}

在生命周期钩子中:

this.route.queryParams.subscribe((params) => {
 console.log(params);
})
英文:

Try ActivatedRoute:

constructor(private route: ActivatedRoute) {}

In the lifecycle hook:

this.route.queryParams.subscribe((params) => {
 console.log(params);
})

答案2

得分: 0

你可以注入 Router 并记录它的 config 属性。但这只适用于预加载的模块,即:

export class AppComponent {
  constructor(private router: Router) {
    console.log(router.config)
  }
}
英文:

You can inject Router and log it's config prop. But its gonna work only for preloaded (not ladyloaded) modules, i.e.

export class AppComponent {
  constructor(private router: Router) {
    console.log(router.config)
  }
}

huangapple
  • 本文由 发表于 2020年1月6日 17:37:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定