constructor(private router: Router){
// override the route reuse strategy
this.router.routeReuseStrategy.shouldReuseRoute = function(){
return false;
}
this.router.events.subscribe((evt) => {
if (evt instanceof NavigationEnd) {
// trick the Router into believing it's last link wasn't previously loaded
this.router.navigated = false;
// if you need to scroll back to top, here is the right place
window.scrollTo(0, 0);
}
});
}
3 回答
对我来说,3个步骤正在帮助解决这个问题 .
"trick the Router"正如Mihail Cuculici所说(在app-component中添加)
将
runGuardsAndResolvers
:"always"添加到路由模块不知道为什么它失败但是当我试图获取解析器构造函数中的所有数据并且只返回
resolve
函数中的数据时它只在我第一次加载组件时起作用,所以我将它全部移动到resolve
函数 .我刚刚碰到同样的问题,找不到合适的解决方案 . 此外,每个人最好的朋友谷歌都没有多少帮助 . 在我的情况下,我需要一个通用的活动表单重置行为,并重新加载当前组件似乎是一个非常好的方法 . 当然,重置表单按钮要好得多,但我看到很多用户都想重新加载页面(通过多次单击同一个链接) .
我设法欺骗 Router 重新加载每个 routerLink 点击组件,AppComponent中有以下代码:
希望这可以帮助
Mihail C.
目前不支持此功能 . https://github.com/angular/angular/issues/9811有人提到有人计划在以后(最终之后)添加更多关于路由组件重用的控制 .
通常你重新初始化就像
如果这不起作用,因为DOM需要重新初始化(例如,因为某些非Angular2库更新了DOM . 您可以使用
*ngIf
清除组件的DOM并重新添加它 .