Errabbah Tarik
Errabbah Tarik

Reputation: 224

Angular 4 : Call AppComponent Method from Another Component

I have a logout bouton in shared page, but i want to call logout method after clicked. the implementation of my logout function is in app.comoponent.ts

can anyone help?

Upvotes: 0

Views: 1105

Answers (3)

Errabbah Tarik
Errabbah Tarik

Reputation: 224

thank you guys, I juste move my login bouton to a new header component and i move my implimentation to the header.comoponent.ts

Upvotes: 0

Mykola Zatonatskiy
Mykola Zatonatskiy

Reputation: 349

There are couple solutions to this question.

1.) You have to follow this steps:

  1. Create a service.
  2. Create a BuheviorSubject(or ReplaySubject) RXJS in service.
  3. In component B on event use next method in Subject (from service).
  4. Subscribe in App component (on Subject from service) to it and do what you want.

2.) You have to follow this steps and Correct one:

  1. Create a service.
  2. Do all http request and so on in service.

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222712

You need to make use of Shared service here. Have your method implemented in a shared service and call the method from your login and shared component.

Upvotes: 1

Related Questions