Angular 4, get route data without actual routing

Is there anything in Angular 4 I can call where I pass a string or an array of route markers and return the static route data for that route?

For instance:

const targetRoute = '/test/route'
const routeData = {{something}}.getRouteData(targetRoute)

// { routeDataValue: 'something' } etc...

The data I'm looking for is the data defined in the route definitions

 const routes: Routes = [
    {
        path: 'test/route',
        data: { //This data object
          animation: {
            value: 'fetching-results'
          },
          progress: 100,
          sectionIdentifier: {
            background: 'results',
            backLinkUrl: null,
            backLinkText: null
          }
        },
    }
]
+4
source share
1 answer

You can get all your routes and route routes (including the data property) from the router, for example:

import { Router } from '@angular/router';

...

constructor(
  private router: Router) {
}

ngOnInit() {
  // all routes
  console.log(this.router.config);

  // data of test/route
  console.log(this.router.config.find(route => route.path === 'test/route').data);
}
+3
source

Source: https://habr.com/ru/post/1686057/


All Articles