Search code examples
angularangular-routingangular-activatedroute

activatedRoute returning empty object


I have a routing module that has routes set up for a number of other modules, that I've managed to use activatedRoute for with no problems. But I've now created a singular component and it is returning an empty object when I try and use activatedRoute.

The routing module looks like something like this:

const routes = [
   {
      path: 'path',
      component: myComponent,
      resolve: { resolver: myCustomResolver },
      children: [
         {
            path: '',
            children: [
               {
                  path: 'users',
                  loadChildren: 'app/users.module#UsersModule'
               },
               {
                  path: 'articles',
                  loadChildren: 'app/articles.module#ArticlesModule'
               }
            ]
         },
         // this is the new component
         { path: 'stories', component: StoriesComponent }
      ]
   }
]

I'm using the same method for activatedRoute in both modules and new component:

export class StoriesComponent implements OnInit {
   private routeData;

   constructor(private activatedRoute: ActivatedRoute) {}

   ngOnInit() {
      // this is returning an empty object '{}'
      this.activatedRoute.data.subscribe(data => {
         this.routeData = data;
      });
   }
}

Any help here would be greatly appreciated.


Solution

  • You want to access parent resolve data in the children. Try this.

    this.routeData = this.activatedRoute.parent.snapshot.data;