1

I am new to angular 2 and want to know which is the best way to pass data between 2 components when a user switches from one view to another.

more elaborately, I have 2 components: login and home. when a user enters the username in the login form and clicks submit, the username should be submitted to home page component and should be stored locally so that when user refresh the page still shows the username.

Jawand Singh
  • 1,929
  • 1
  • 24
  • 21
Jay
  • 397
  • 1
  • 5
  • 16
  • 1
    https://angular.io/docs/ts/latest/cookbook/component-communication.html Shared service would be the way to go since I assume these are same level components? – AT82 Apr 24 '17 at 12:23
  • yes same level components – Jay Apr 24 '17 at 12:42
  • link which you shared has a parent child combination – Jay Apr 24 '17 at 12:43
  • Yes, but the idea is the same, shared service :) You cannot use the other like `Input` since your components are same level components. – AT82 Apr 24 '17 at 12:46

1 Answers1

1

Communication between 2 components can be done in a few ways:

  1. Via Input/Output Bindings

  2. Via Services

  3. Via Eventemitters

Please refere to the offical Angular documentation: https://angular.io/docs/ts/latest/cookbook/component-communication.html

Emre Öztürk
  • 2,660
  • 4
  • 16
  • 19