gpt4 book ai didi

angular - 路由器导航在同一页面时不调用 ngOnInit

转载 作者:太空狗 更新时间:2023-10-29 16:46:27 32 4
gpt4 key购买 nike

我在同一页面上使用一些查询字符串参数调用 router.navigate。在这种情况下,ngOnInit() 不会调用。是默认设置还是我需要添加任何其他内容?

最佳答案

您可以注入(inject) ActivatedRoute 并订阅 params

constructor(route:ActivatedRoute) {
route.params.subscribe(val => {
// put the code from `ngOnInit` here
});
}

路由器只有在导航到不同的路由时才会销毁并重新创建组件。当只更新路由参数或查询参数但路由相同时,组件不会被销毁和重新创建。

强制重新创建组件的另一种方法是使用自定义重用策略。另见 Angular2 router 2.0.0 not reloading components when same url loaded with different parameters? (似乎没有太多可用的信息,但如何实现它)

关于angular - 路由器导航在同一页面时不调用 ngOnInit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41678356/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com