Search code examples
angularangular2-routing

How to get current route custom data in angular 2?


I have set up routes is like below

const appRoutes: Routes = [
  {
    path: 'login',
    component: LoginComponent,
    data: {
      title: 'Login'
    }
  },
  {
    path: 'list',
    component: ListingComponent,
    data: {
      title: 'Home Page',
      module:'list'
    }
  },
  {
    path: '',
    redirectTo: '/login',
    pathMatch: 'full'
  },
];

now when i come to '/list' route then on 'listing.component.ts' i have written below code

export class ListingComponent {

  public constructor(private router:Router) {
      //here how i can get **data** of **list** routes

  }
}

Solution

  • public constructor(private route:ActivatedRoute, private router:Router) {
      console.log(route.snapshot.data['title'])
    }