Search code examples
angularmodulecomponentssend

In Angular, how to pass value from one component to another when the components are not parent/child?


I am new in Angular2+ and reading a lot of materials inclusive of Angular site.

From angular, for parent/child we can use @Input() https://angular.io/guide/template-syntax#input-and-output-properties

But in my code, I have two different modules, and each one has one component.

How to pass, object value from the component1 of the Module1 to the component2 of the Module2?

I have tried with @Input() but no success, and from the Argular link above @Input() refers to parent/child which this is not my case, well, from my understanding it's not :)

I can send through Route, string and number values, but not objects, that's the reason I need a different approach.

Thanks for any comments Ademir


Solution

  • As mentioned in the comments, you could use a fully-featured state manager like Redux.

    If you have a fairly simple case, however, you can just create a service to hold the information, and inject the service into both components.

    This will require that the service be in a scope that is available to both components.