我有一个angular13应用程序,在这个应用程序中,我通过route data将数据从一个component传递到另一个component。在我的componentA中,我有一个函数
navigateTo(data: any){
this._router.navigateByUrl(url, { state: data}); // {id: 1, age: 3} //
}在我的componentB中,我以这种方式访问数据
constructor(
private router: Router,
private route: ActivatedRoute,
private location: Location
) {
if (this.router.url.includes('edit')) {
this.isEdit = true;
console.log(this.location.getState());
}
}但它总是用console在componentB中打印。
{navigationId: 6 //(random id each time)}为什么数据不能正确传递?
发布于 2022-03-11 06:35:50
我没有理解您的问题,因为我猜"this.location.getState()“不是获取数据的正确方法(至少,以”角度“的方式)。
试试这个:
console.log(this.router.getCurrentNavigation().extras.state.id)更多信息这里
https://stackoverflow.com/questions/71427018
复制相似问题