Angular2 passing data between two child components

南楼画角 提交于 2019-12-11 05:42:12

问题


I have a component with two childcomponents. I show one or the other with using the router with the router-outlet.

I want to take form data inserted in the first child, and use it in the other child which I route to upon submitting the form.

I suspect I need to use @input or @output but I can't find any documentation on how to do it when using the router-outlet.

My current other solution is to save the data in the localstorage when submitting the form in the first child and calling it in the second child. Doesn't seem ideal.


回答1:


You could pass the data via a service. If you create a service, and set the data in child A, you could then retrieve it from child B. The only catch is you need to set this service you create as a provider for the parent class, and not for the child components.

Here is an example of this Angular 2 passing object via route params possible?




回答2:


You could use @ngrx/store to store data, then inject those into your route components. See the examples there for implementation.



来源:https://stackoverflow.com/questions/38254379/angular2-passing-data-between-two-child-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!