我有一个页面,单击该按钮后,它将使用以下路由器导航进行重定向
router.navigate (['search', {data: 'test'}]).
Run Code Online (Sandbox Code Playgroud)
但是,当我第二次单击同一按钮而不更改值时,router.navigate将无法工作。我该如何覆盖。
欢迎所有想法!
this._router.routeReuseStrategy.shouldReuseRoute = function(){
return false;
};
this._router.events.subscribe((evt) => {
if (evt instanceof NavigationEnd) {
this._router.navigated = false;
window.scrollTo(0, 0);
}
});
Run Code Online (Sandbox Code Playgroud)
我将此添加到我的app.component.ts ngOnInit函数中,并且运行良好。现在对同一链接的所有进一步点击都会重新加载component和数据。
幸得mihaicux2 GitHub上。
我在版本4.0.0-rc.3上测试了这个import { Router, NavigationEnd } from '@angular/router';
Angular路由器以这种方式工作,因此,如果未更改activeRoute,则不会再次加载页面/组件。
如果您要传递参数,例如在示例中:{data:'test'},则可以通过订阅路由参数来观察这些参数的变化:
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.route.params.subscribe(params => {
// each time the search data is change you'll get this running
//Do what ever you need to refresh your search page
console.log('New route params');
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5167 次 |
| 最近记录: |