SkinnyBetas
SkinnyBetas

Reputation: 501

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.

Upvotes: 2

Views: 2429

Answers (2)

coturiv
coturiv

Reputation: 2970

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

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

Upvotes: 4

IAfanasov
IAfanasov

Reputation: 4993

From Angular documentation:

data: Observable<Data> - An observable of the static and resolved data of this route.

In the provided routing configuration there is no static data or dynamic data resolver. That is why data is an empty object.

Upvotes: 1

Related Questions