Angular 2可以沿路由器传递复杂对象吗?

Mac*_*tyn 7 angular2-routing angular

是否可以通过路由器发送复杂对象?这是我正在做的和尝试做的事情:

在搜索页面中,用户可以单击其中一个结果上的按钮,该结果调用导致此行触发的方法. this.router.navigate(['profile-detail', selection]);

selection物体看起来像这样只是导航之前.

{
  profile: {
    id: number,
    fname: string,
    lname: string
  },
  contact: {
    type: string,
    address: string
  },
  books: [{
    title: string,
    author: string
  }]
}
Run Code Online (Sandbox Code Playgroud)

但是,当我们到达profile-detail页面时,将this.route.snapshot.params其作为实际数据:

{
  profile: "[object Object]",
  contact: "[object Object]",
  books: "[object Object]"
}
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 6

不,路由器需要将数据序列化到浏览器URL栏,并且URL栏仅支持字符串.

您可以使用共享服务在不是父/子或兄弟节点的组件之间传递对象,也可以像路由器那样动态添加对象.

另见https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

根据您的具体要求,解析器也可以工作https://angular.io/docs/ts/latest/guide/router.html#!#resolve-guard