我正在使用 navigatorByUrl 方法传递数据,并希望在目标组件 ngOnInit() 内访问它。
this._router.navigateByUrl(
${baseUrl}?${previousFilter}
, { state: {from:'details-page'} });
我正在传递数据,如上所示。那么如何访问目标组件的状态值 ngOnInit() 呢? (使用 ActiedRoute 或任何其他标准方法)。
使用navigateByUrl()实现此目的的正确方法是
根据 Angular 文档,您可以使用ActivatedRoute API 来完成此任务。
在他们的文档中,您可以找到这个示例,它与您正在做的非常相似:
constructor(private route: ActivatedRoute) {}
ngOnInit() {
const heroId = this.route.snapshot.paramMap.get('id');
}
gotoItems(hero: Hero) {
const heroId = hero ? hero.id : null;
// Pass along the hero id if available
// so that the HeroList component can select that item.
this.router.navigate(['/heroes', { id: heroId }]);
}