Angular2路由器2.0.0在不同的参数加载相同的url时不重装组件? [英] Angular2 router 2.0.0 not reloading components when same url loaded with different parameters?

查看:112
本文介绍了Angular2路由器2.0.0在不同的参数加载相同的url时不重装组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  export const routing = RouterModule.forChild([
{
path:'page /:id',
component:PageComponent
}]);

我的 PageComponent 文件检查id参数和相应地加载数据。在以前版本的路由器中,如果我从/ page / 4到/ page / 25,页面将重新加载并且组件会更新。



我尝试导航到/ page / X,其中X是id,它只加载第一次,然后url更改,但组件不会再次重新加载。



有什么需要传入来强制我的组件重新加载并调用ngOnInit事件吗?

解决方案

strong> update 2



这个答案仅适用于很久以前停止使用的路由器版本。



请参阅 https://angular.io/api/router/RouteReuseStrategy 了解如何在当前路由器。

更新1



现在已经修复(Angular 2.3)通过 https:// git hub.com/angular/angular/pull/13124 ,它允许提供自定义重用策略。



例如,另请参阅 https://www.softwarearchitekt。 at / post / 2016/12/02 / sticky-routes-in-angular-2-3-with-routereusestrategy.aspx

original



https:/ /github.com/angular/angular/issues/9811



这是一个已知的问题。
目前没有办法让路由器只在参数路由改变时重新创建组件。他们讨论了最终实现一些重用策略的计划。



您可以订阅参数更改并在那里执行您的代码而不是 ngOnInit()


I have this in my .routing.ts file

export const routing = RouterModule.forChild([
{
    path: 'page/:id',
    component: PageComponent
}]);

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.

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.

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.

See https://angular.io/api/router/RouteReuseStrategy for how to do it in the current router.

update 1

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.

For an example see also https://www.softwarearchitekt.at/post/2016/12/02/sticky-routes-in-angular-2-3-with-routereusestrategy.aspx

original

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.

You can subscribe to params changes and execute your code there instead of in ngOnInit()

这篇关于Angular2路由器2.0.0在不同的参数加载相同的url时不重装组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆