简体   繁体   English

Angular 4:从另一个组件调用AppComponent方法

[英]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 我的注销功能的实现在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.)您必须遵循以下步骤:

  1. Create a service. 创建服务。
  2. Create a BuheviorSubject(or ReplaySubject) RXJS in service. 在服务中创建BuheviorSubject(或ReplaySubject)RXJS。
  3. In component B on event use next method in Subject (from service). 在事件的组件B中,使用Subject(来自服务)中的下一个方法。
  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: 2.)您必须按照以下步骤操作并更正一个:

  1. Create a service. 创建服务。
  2. Do all http request and so on in service. 在服务中执行所有http请求等等。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 完全加载视图(AppComponent)后如何从另一个组件调用方法? - How to call a method from another component after view is fully loaded (AppComponent)? 如何从 Angular 中的另一个组件调用方法? - How to call a method from another component in Angular? 角度2:如何从另一个组件调用方法 - Angular 2: How to call a method from another component 如何在 AppComponent 以外的其他组件中使用从共享模块导入的 Angular 组件 - How can I use angular component imported from a shared module in another component other than AppComponent 在Angular 7中将工厂方法从AppModule移至AppComponent - Move factory method from AppModule to AppComponent in Angular 7 如何从另一个组件调用一个组件中的方法是 angular2 - how to call a method in one component from another component is angular2 如何从角度2中的另一个组件调用组件方法? - how to call a component method from another component in angular 2? 如何以角度从一个组件调用另一个组件的方法 - How to call a method from one component to another in angular 如何从另一个组件angular2调用方法 - How to call method from another component angular2 从Angular2中的AppComponent将参数传递给子组件或对等组件 - Passing parameter to child or peer component from AppComponent in Angular2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM