问题描述
我的 .routing.ts 文件中有这个
I have this in my .routing.ts file
export const routing = RouterModule.forChild([
{
path: 'page/:id',
component: PageComponent
}]);
我的 PageComponent
文件检查 id 参数并相应地加载数据.在以前版本的路由器中,如果我从/page/4 转到/page/25,页面会重新加载"并且组件会更新.
My PageComponent
file checks the id parameter and loads the data accordingly. In previous versions of the router, if i went from /page/4 to /page/25, the page would "reload" and the components would update.
现在,当我尝试导航到 X 是 ID 的/page/X 时,它只会在第一次加载,然后 url 更改,但组件不会再次重新加载".
Now when i try navigating to /page/X where X is the id, it only ever loads the first time, then the url changes, but the components do not "reload" again.
是否需要传入一些东西来强制我的组件重新加载,并调用 ngOnInit 事件?
Is there something that needs to be passed in to force my components to reload, and call the ngOnInit event?
推荐答案
update 2
这个答案只针对很久以前停产的路由器版本.
This answer is only for a long ago discontinued router version.
查看 https://angular.io/api/router/RouteReuseStrategy 了解如何操作它在当前路由器中.
See https://angular.io/api/router/RouteReuseStrategy for how to do it in the current router.
更新 1
现在通过 https://github.com/为新路由器修复了(Angular 2.3)angular/angular/pull/13124 允许提供自定义重用策略.
That's now fixed (Angular 2.3) for the new router by https://github.com/angular/angular/pull/13124 which allows to provide a custom reuse strategy.
原创
https://github.com/angular/angular/issues/9811
这是一个已知问题.目前没有办法让路由器在仅参数路由更改时重新创建组件.
That's a known issue.Currently there is no way to make the router re-create the component on parameter-only route changes.
他们讨论了最终实施一些重用策略的计划.
They discussed plans to implement some reuse-strategies eventually.
您可以订阅参数更改并在那里执行您的代码,而不是在 ngOnInit()
You can subscribe to params changes and execute your code there instead of in ngOnInit()
这篇关于Angular2 路由器 2.0.0 在使用不同参数加载相同 url 时不重新加载组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!