是否可以通过路由器发送复杂对象?
这是我正在做的和尝试做的事情:
这是我正在做的和尝试做的事情:
在搜索页面中,用户可以单击其中一个结果上的按钮,该结果调用导致此行触发的方法. this.router.navigate([‘profile-detail’,selection]);
选择对象在导航之前就像这样.
{ profile: { id: number,fname: string,lname: string },contact: { type: string,address: string },books: [{ title: string,author: string }] }
但是,当我们到达profile-detail页面时,this.route.snapshot.params将此作为实际数据:
{ profile: "[object Object]",contact: "[object Object]",books: "[object Object]" }
解决方法
不,路由器需要将数据序列化到浏览器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,解析器也可能有效