soulfresh
我在这个 GitHub 问题中建议了我找到的解决方案: https ://github.com/angular/angular/issues/18798
我这样称呼我的路线:
this.router.navigateByUrl(this.router.createUrlTree([RoutesUrl.ERROR], {
queryParams: {
errorTitle: 'Erreur Technique',
errorBody: 'URL erronée'
}
}));
然后我以这种方式检索查询参数:
this.activatedRoute.queryParamMap.subscribe(params => console.log(JSON.stringify(params)))
或者
this.activatedRoute.queryParams.subscribe(params => console.log(JSON.stringify(params)))
更新
如果您不希望您queryParams
出现在地址栏 (URL) 中,您可以使用:
发送数据:
constructor(private router: Router) {}
ngOnInit(){
this.router.navigate([RoutesUrl.ERROR], {
state: {
errorTitle: 'Erreur Technique',
errorBody: 'URL erronée'
}
});
}
检索数据:
constructor(private router: Router) {
this.errorTitle = this.router.getCurrentNavigation().extras.state.errorTitle;
this.errorBody = this.router.getCurrentNavigation().extras.state.errorBody;
}