如果它们不是Angular2中的父子组件,如何在组件之间传递数据? [英] How to pass data between components, if they are not parent-child components in Angular2?

查看:84
本文介绍了如果它们不是Angular2中的父子组件,如何在组件之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个组件,它们不是父级组件,只是独立组件.我想将数据从Component1传递到Component2. 是否有可能?我该如何实施呢? 您能给我一个例子还是文章的链接? 我找到了很多文章,但它们都是关于父子组件的.

I have 2 Components, they are not child-parent components, just to independent components. I would like to pass data from Component1 to Component2. Is it possible? How could I implement this? Could you please give me an example or maybe a link to an article? I found a lot of articles, but they are all about parent-child components.

推荐答案

如果组件之间没有父子关系,则可以在两个选项之间传递数据

You have two options here to pass data between components if they donot have a parent child relationship

  • 共享服务
  • Ngrx Suite(Redux)

现在何时使用,如果您的应用程序大小是中小型,则可以使用行为主题或重播主题来使用共享服务. 请检查此链接以了解有关行为主题以及如何使用共享服务在组件之间进行通信的更多信息

Now when to use when if you application size is medium to small go for shared services using behaviour subject or replay subjects. please check this link to know more about behaviour subject and how to use shared services to communicate between components.

使用Ngrx就像是针对此问题的完整的长期解决方案.它遵循redux模式来解决Angular中的状态管理. 请点击此链接以获取有关ngrx以及如何在项目中使用它们的更多信息.

Using Ngrx is like a complete long term solution to this problem. It follows the redux pattern to solve state management in Angular. follow this link for more on ngrx and how to use them in a project .

有关 ngrx

这是一个使用整个ngrx套件状态管理的实时项目 footballapp

This is a live project using the whole ngrx suite state management footballapp

这篇关于如果它们不是Angular2中的父子组件,如何在组件之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆