首页 > 解决方案 > 在 routerLink 中传递自定义对象

问题描述

我想将 2 个参数传递给[routerLink]astring和 anobject

<a [routerLink]="[questionDetailRouterLink, {'question-id':question['question-id'],'question-list':this.questions}]">Show more</a>

其中 questionDetailRouterLinkurl/ stringquestion['question-id']映射到 astring并且this.questions是 aobject类。所以被作为它的等价物this.questions传递,即toString()[Object object]

在接收组件方面,我试图提取这样的值

this.question_id = this.route.snapshot.paramMap.get('question-id'); // question-details;question-id=:id'
this.questions = this.route.snapshot.paramMap.get('question-list'); //this gives [Object object]

我以为我可以传递JSON.stringify对象的版本(并JSON.parse在接收端执行)但我不能在模板中执行此操作[routerLink]="[questionDetailRouterLink, {'question-id':question['question-id'],'question-list':JSON.parse(this.questions)}]"- 编译错误

如何将对象传递给另一个对象(有独立的并且不是父/子)所以我也不能使用input参数

标签: angular6

解决方案


我读到了两个解决我的问题的选项。第一个是使用provider. 在路由到 之前Component A更新中的值。有一个变量来保存要传递的对象,该对象更新然后读取。其他选项是在 A 和 B 之间创建父/子关系,并在需要时使用动态元素创建来显示 B ( ,等)provider Pcomponent BProvider PABngContainerngTemplatecreateEmbeddedView


推荐阅读