angular - 如果路由到相同的参数,角度不会重新加载
问题描述
我想在article/:id
每次单击按钮时重新加载页面,Angular 能够在:id
更改时重新加载页面,但如果路由到相同:id
,那么它会忽略更改。
export class DocumentComponent {
constructor(
private activatedRoute: ActivatedRoute,
private router: Router
) {}
ngOnInit() {
this.routeSubscription = this.activatedRoute.params.subscribe(params => {
this.reloadPage(+params['id']);
});
}
clickReload(id: number) {
this.router.onSameUrlNavigation = 'reload';
this.router.navigate(['article', id]);
}
}
当参数改变时,params
将触发可观察对象,并reloadPage()
执行。但是,当我尝试重新加载相同的params
内容时,Angular 会忽略它,我该如何重新加载相同的内容param
?
article/123 --> article/456 [ok]
article/111 --> article/222 [ok]
article/666 --> article/666 [not ok, how?]
我尝试使用onSameUrlNavigation
,但它不起作用......我做错了吗?
解决方案
您需要检查 id 是否相同,然后重新加载页面。你可以这样做:
clickReload(id: number) {
if(id == this.activatedRoute.snapshot.params.id) {
window.location.reload(); //if id is same then just reload the page
} else {
this.router.navigate(['article', id]);
}
}
推荐阅读
- java - 如何传入/使用 .Class/Object 作为方法参数
- excel - 操作 CSV 文件:增加单元格坐标/位置
- machine-learning - `BCEWithLogitsLoss` 和 Pytorch 中的训练类数据集不平衡
- python-3.x - Python 列表格式化和更新
- php - Laravel 5.6 路由组
- python - virtualenv 中的 pip 版本错误
- javascript - 如何像参数一样在数组中使用 .map() 数组?
- go - 如何模拟另一个包中的函数?
- r - 使用来自 openssl 的 rsa_decrypt 解密 R 中的密钥时出错
- vb.net - VB.Net 暂停 Windows 关机或注销