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

  }
}

Answer

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

Leave a Reply

Your email address will not be published. Required fields are marked *