如何将自定义数据添加到路由?

问题描述:

我想在定义路线时添加一些自定义数据来进行路由.

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

我该怎么做?

like:

{
  path: 'department',
  component: DepartmentComponent,

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

我不希望自定义数据显示在URL中.我只是在内部使用.

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

您可以像这样为路由定义自定义数据:

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

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

阅读如下:

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

它是在Route界面上定义的:

export interface Route {
  path?: string;
  ...
  data?: Data;
}