0

In Angular 10 I have the following entry inside my routes:

const routes = [
  { path:'my/target/:targetId', component: MyTargetComponent, data: { dataKey: 'dataValue} },
   ...
]

From anywhere inside my application, given any path (say my/target/55) I need to be able to access all information contained inside the Route object that I used to define such route (in particular I need to read the data field). Is there a built-in function in Angular 10 to do this?

UPDATE: I need to access information for urls that are NOT necessarily the url of the current page.

Sirion
  • 804
  • 1
  • 11
  • 33
  • Does this answer your question? [How to get current route custom data in angular 2?](https://stackoverflow.com/questions/40863664/how-to-get-current-route-custom-data-in-angular-2) – Freeky Jul 20 '20 at 10:37
  • Unfortunately no, I've updated my question. – Sirion Jul 20 '20 at 10:52

2 Answers2

-1

The Angular Router will pass the dynamic data. supose, data:{ id:'1', name:"Angular10"} when the MyTargetComponent is rendered. The data value will be located in the data property of the ActivatedRoute service

We can then read the data by subscribing to the activatedroute.data property as shown below:

   ngOnInit() { 
   this.activatedroute.data.subscribe(data => {
       this.product=data;
   }) }
smita
  • 298
  • 1
  • 4
  • 17
-2

Try like this

constructor(private route: Router) {}
ngOnInit() {
    console.log(this.router);
    // on console expand this object and will get all details of routes of complete application in **this.router.config** object.
}
Santosh Shinde
  • 1,206
  • 10
  • 16