首页 > 解决方案 > Angular 8 - 导航到相同的路由路径时处理路由订阅

问题描述

我正在从后端应用程序接收路由通知,以加载 Angular 组件。例子:

// Current route "/example1/path1"
let route = "/example2/path1"; //route received from back-end
this.router.navigate([route]); // OK: navigates to component associated to "/example2/path1" path

此外,在每个组件中,我都在订阅中处理路由通知:

this.route.params.subscribe(params => {
...
            }
        });

尽管如此,我在一些接收到的路线上导航到相同的组件时遇到了一些麻烦,比如说:

// Current route "/example1/path1"
let route = "/example1/path1"; //route received from back-end (SAME ROUTE PATH)
this.router.navigate([route]); // NOK: route notification not received in subscription

我的问题是:当应用程序需要导航到同一条路线时,是否有任何方法可以接收此订阅通知?查询参数对我来说是一个有效的解决方案,例如:“/example1/path1?timestamp=1234567”,但是我不知道如何在不创建单独的解析方法的情况下分别解析“timestamp”和“path1”......

我将不胜感激有关此的任何帮助或建议。

标签: angulartypescriptroutesangular-routingangular-router

解决方案


https://angular.io/guide/router#getting-route-information

this.router.navigate(["/example1/path1"],{timestamp: 1234567});
import {ActivatedRoute} from "@angular/router";

constructor(private route: ActivatedRoute) {} 

ngOnInit() {
 // "/example1/path1?timestamp=1234567"
  this.route.params.subscribe(params => {
    this.timestamp = params['timestamp'];
  });
}

推荐阅读