Search code examples
angularroutesangular-activatedroute

Angular - How to subscribe to child route params only when the child route is activated


I have a split screen design. I'd like to access the folder ID from the parent route only when the child route is activated. The below works in getting the correct params for me, but on the initial load where I'm only displaying the parent route, I get a console error:

Cannot read property 'params' of null

this.activatedRoute.firstChild.params.subscribe((urlParameters) => {
  this.folder_id = urlParameters['folderId'];
});

Is there a way to only activate the above once the child has been activated?


Solution

  • you can simply try

     if(this.activatedRoute.firstChild) 
       this.activatedRoute.firstChild.params.subscribe((urlParameters) => { 
         this.folder_id= urlParameters['folderId']; 
       });