Get param from parent component to child component

纵饮孤独 提交于 2019-12-06 11:34:53

In Angular 5.2 release there is new feature of paramsInheritanceStrategy ,that can help you for your problems now.

You can use it as following

@NgModule({
    import :[RouterModule.forRoot(router,{paramsInheritanceStrategy :'always'})]
})

It defines how the router merges params, data and resolved data from parent to child

Available options are:

1. emptyOnly: the default , only inherits parent params for path-less or component-less

2. always: enables unconditional inheritance of parent params.

In component you can use it as follows:

 constructor(private route: ActivatedRoute) {}

 ngOnInit() {
    this.eventId = +this.route.snapshot.paramMap.get("eventId");
  }

Thanks for all your help. The answer was very lengthy. Without subscription the line was:

this.eventId = this.activatedRoute.parent.snapshot.parent.params.eventId;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!