首页 > 解决方案 > 如果路由到相同的参数,角度不会重新加载

问题描述

我想在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,但它不起作用......我做错了吗?

标签: angular

解决方案


您需要检查 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]);
    }
}

推荐阅读