user4965201
user4965201

Reputation: 973

Not able to find route custom data in angular 6 in app component

I have an angular application and added custom data to the route given as

{ path: 'profile', component: ProfileComponent, data: {title: 'example'} }

and in app.component.ts file I have the following code as

private route: ActivatedRoute

console.log('route snapshot', this.route.firstChild.data._value.pageType );

the error i am getting is

ERROR in src/app/app.component.ts(71,64): error TS2339: Property '_value' does not exist on type 'Observable<Data>'.

can anyone tell me how to access data of route in app.component.ts

Upvotes: 0

Views: 86

Answers (1)

yazantahhan
yazantahhan

Reputation: 3110

Update

You will need to subscribe to the router events in the AppComponent and get the params from it:

constructor(private router: Router, route: ActivatedRoute) { }

ngOnInit() {
  this.router.events.subscribe(event => {
    if(event instanceof NavigationEnd){
      console.log(this.route.root.firstChild.snapshot.data['title']);
    }
  });
}

If you want it only for that path, you can add one more condition

if(event instanceof NavigationEnd && event.url === '/profile')

Old Answer

You will need to access the snapshot to get the data params

this.route.snapshot.data['title'];

Upvotes: 1

Related Questions