Dmitry Grinko
Dmitry Grinko

Reputation: 15214

How can I take variable "data" from Routes in Angular 4?

Could you give me an example how to get data from Routes. Routing & Navigation

const appRoutes: Routes = [
  { path: 'crisis-center', component: CrisisListComponent },
  { path: 'hero/:id',      component: HeroDetailComponent },
  {
    path: 'heroes',
    component: HeroListComponent,
    data: { title: 'Heroes List' } // <--- How to use this data?
  },
  { path: '',
    redirectTo: '/heroes',
    pathMatch: 'full'
  },
  { path: '**', component: PageNotFoundComponent }
];

Thank you

Upvotes: 0

Views: 678

Answers (1)

Emin Laletovic
Emin Laletovic

Reputation: 4324

You can use ActivatedRoute in your component/service constructors.

For example:

public class MyAppComponent{
data: any;   

constructor(route: ActivatedRoute){
      this.data = route.snapshot.data;
   }
}

You can find more details here.

Upvotes: 2

Related Questions