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

编程入门 行业动态 更新时间:2024-10-26 06:37:31
本文介绍了路由器导航在同一页面时不调用ngOnInit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am calling router.navigate on same page with some query string parameters. In this case, ngOnInit() does not call. Is it by default or do I need to add anything else ?

推荐答案

您可以注入ActivatedRoute并订阅params

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

路由器仅在导航到其他路线时销毁并重新创建组件.当仅更新路由参数或查询参数但路由相同时,该组件将不会被销毁和重新创建.

The router only destroys and recreates the component when it navigates to a different route. When only route params or query params are updated but the route is the same, the component won't be destroyed and recreated.

强制重新创建组件的另一种方法是使用自定义重用策略.另请参阅当使用不同的参数加载相同的url时,Angular2路由器2.0.0不会重新加载组件吗?(似乎没有太多可用的信息来实现它)

An alternative way to force the component to be recreated is to use a custom reuse strategy. See also Angular2 router 2.0.0 not reloading components when same url loaded with different parameters? (there doesn't seem to be much information available yet how to implement it)

更多推荐

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

本文发布于:2023-11-16 19:59:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607252.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由器   页面   在同一   ngOnInit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!