NavigationEnd 订阅 route.snapshot

NavigationEnd .subscribing to route.snapshot

大家好,想知道您是否可以给我一些指示?我目前正在尝试获取 URL 并动态显示一个函数。

this._mont.showMap(+this._route.snapshot.params['id']));

每当我使用这行代码时,showMap() 函数就会在当前索引上被捕获。我想使用 NagivationEnd 路由器事件来获取下一个索引。

this.router.events.filter((routerEvent) => routerEvent instanceof NavigationEnd)
  .subscribe(() => this._mont.showMap(+this._route.snapshot.params['id']));
}

我想知道我是否正确地处理了这个问题或者是否有更好的方法来处理它?

虽然这不是完全错误的方法,但我建议订阅 ActivatedRoute:route.paramMap。这样,该函数仅在 id 参数更改时被调用,这比每次 url 更改时调用它更有效。 编辑: 您也可以直接从订阅结果中获取 ID(如 result => result.get('id'))。编辑 2:使用 .get() 方法。