angular6 - 在 routerLink 中传递自定义对象
问题描述
我想将 2 个参数传递给[routerLink]
astring
和 anobject
<a [routerLink]="[questionDetailRouterLink, {'question-id':question['question-id'],'question-list':this.questions}]">Show more</a>
其中
questionDetailRouterLink
是url
/ string
,question['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
参数
解决方案
我读到了两个解决我的问题的选项。第一个是使用provider
. 在路由到 之前Component A
更新中的值。有一个变量来保存要传递的对象,该对象更新然后读取。其他选项是在 A 和 B 之间创建父/子关系,并在需要时使用动态元素创建来显示 B ( ,等)provider P
component B
Provider P
A
B
ngContainer
ngTemplate
createEmbeddedView
推荐阅读
- python - 如果列表作为 y 传递,Keras 中 y_true 的形状是什么?
- r - 在 R 中的字符串上使用 If / Else if 时出现错误
- python - 如何在数据框中找到另一个没有循环的相关行
- css - 链接和文本的 CSS 位置集中在特定的屏幕尺寸/分辨率上
- r - 如何计算更快的月度加权平均值
- php - 刚刚升级了 Bitnami WAMP 堆栈,现在我的 PHP 文件正在忽略文件更改
- python - 连接字符串的 Pandas keyerror
- macos - “更改目录”命令在 Mac 终端中不起作用
- angular - 即使在召回功能角度上,循环也不会停止
- docker - 从 git 克隆的构建代码在 dockerfile 中失败