Angular2路由器:为什么我们不再在路由上拥有状态

时间:2016-10-18 15:18:52

标签: angular typescript

这是我的app.routes

const routes: Routes = [
    {
      path: "login",
      component: LoginComponent
    },
    {
      path: "users",
      component: UsersComponent
    },
    {
      path: "error",
      component: ErreurComponent
    },
    {
      path: "more/complex/path",
      component: FooComponent
    },
    {
      path: "",
      component: HomepageComponent,
    }
]

export const APP_ROUTER_MODULE = RouterModule.forRoot(routes)

为什么我不能为路线指定州或名称?如果我想知道我是否在错误页面上,那么我必须检查当前路线并查找我在“路径”中指定的内容(另请参阅上面的FooComponent)。我发现这有点傻。无论如何要解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

const routes: Routes = [
    {
      path: "login",
      component: LoginComponent,
      data: { name: 'r1'}
    },
    {
      path: "users",
      component: UsersComponent,
      data: { name: 'r2'}
    },
    {
      path: "error",
      component: ErreurComponent,
      data: { name: 'r3'}
    },
    {
      path: "more/complex/path",
      component: FooComponent,
      data: { name: 'r4'}
    },
    {
      path: "",
      component: HomepageComponent,
      data: { name: 'r5'}
    }
]
class SomeComponent {
  constructor(route:ActivatedRoute) {
    route.data.forEach(data => console.log(data['name']));
  }
}

另见How do I pass data in Angular 2 components while using Routing?