简体   繁体   中英

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?

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.

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.

谢谢大家,我只是将登录按钮移动到新的标头组件,并将隐含内容移至header.comoponent.ts

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