在最新版本的ANGLE 7.2.6中,我try 在路由本身中传递数据
this.router.navigate(['other'], {state: {someData: 'qwert'}}
在OtherComponent
文件中,this.router.getCurrentNavigation()
始终返回null
在最新版本的ANGLE 7.2.6中,我try 在路由本身中传递数据
this.router.navigate(['other'], {state: {someData: 'qwert'}}
在OtherComponent
文件中,this.router.getCurrentNavigation()
始终返回null
你调用方法getCurrentNavigation
太晚了.导航已经完成.
您需要在构造函数中调用getCurrentNavigation
方法:
constructor(private router: Router) {
this.name = this.router.getCurrentNavigation().extras.state.someData;
}
或者,如果您想在ngOnInit
中访问导航,可以执行以下操作:
ngOnInit() {
this.name = history.state.someData;
}