Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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    } } 
like image 947
Rituraj ratan Avatar asked Nov 29 '16 10:11

Rituraj ratan


People also ask

How can you get the current state of a route in Angular?

There are many ways by which you can get a current Route or URL in Angular. You can use the router service, location service or window object to get the path. You can also listen to changes to URL using the router event or URL change event of the location service.

How do I get the current URL in ActivatedRoute?

You can use the activated route to get active URL. (<RouterStateSnapshot>activatedRoute['_routerState']). url , if you like to type it.


2 Answers

public constructor(private route:ActivatedRoute, private router:Router) {   console.log(route.snapshot.data['title']) } 
like image 101
Günter Zöchbauer Avatar answered Sep 20 '22 06:09

Günter Zöchbauer


For Angular 4+

If you place the following code in the parent or upper level components like AppComponent then it won't work. It only works on child or lower level components for which you've defined the route custom data:

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

So, if you want to access the route custom data globally from a parent or upper level component to access the change in the route custom data then you've to listen to router events particularly RoutesRecognized or NavigationEnd events. I'm gonna show two procedures with AppComponent with two events:

First Approach:

   export class AppComponent implements OnInit, AfterViewInit {      constructor(private activatedRoute: ActivatedRoute, private router: Router) { }      ngOnInit() {          this.router         .events         .filter(event => event instanceof NavigationEnd)         .map(() => {           let child = this.activatedRoute.firstChild;           while (child) {             if (child.firstChild) {               child = child.firstChild;             } else if (child.snapshot.data && child.snapshot.data['custom_data']) {               return child.snapshot.data['custom_data'];             } else {               return null;             }           }           return null;         }).subscribe( (customData: any) => {           console.log(customData);        });     }  } 

Second Approach is using:

this.router.events .filter(event => event instanceof RoutesRecognized) .map( (event: RoutesRecognized) => {     return event.state.root.firstChild.data['custom_data']; }) .subscribe(customData => {     console.log(customData); }); 

Note: Even though last one is shorter and usually works, but, if you have nested routes then it is encouraged to use the first one.

like image 40
asmmahmud Avatar answered Sep 22 '22 06:09

asmmahmud