3

I want to add some custom data to route when I define the routes.

How can I do that?

like:

{
  path: 'department',
  component: DepartmentComponent,

  customdata: {
    name: 'foo',
    age: '23'
  }
}

I don't want the custom data to display in URL. I just use it internally.

Lin Du
  • 88,126
  • 95
  • 281
  • 483

2 Answers2

6

You can define the custom data to the route like this:

[
   {path: 'inbox', data: {name: 'foo', age: 23}},
]

and read like this:

class ConversationCmp {
    constructor(r: ActivateRoute) {
         r.data.subscribe((p) => {
              console.log(p);
         });
    }
}

It's defined on the Route interface:

export interface Route {
  path?: string;
  ...
  data?: Data;
}
Lin Du
  • 88,126
  • 95
  • 281
  • 483
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
2

I do it in the following way:

{ path: 'admin', canActivate: [RoleGuard], data: { roles: ['admin'] } }

I use it to add roles custom property to the route to be able to enable access only to users with a particular role, that I'm setting in Routes.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
filipows
  • 456
  • 5
  • 3
  • 1
    That's a good use case! Just something you might be interested in: Cucumber tag-expressions, so you could add roles like '(admin and super) not (guest)' and process an array of roles associated with the user – Drenai May 04 '19 at 10:14