简体   繁体   中英

How to pass data between components, if they are not parent-child components in Angular2?

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

  • Shared Services
  • 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.

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 .

More on ngrx

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM