简体   繁体   English

如何从另一个组件调用组件的ngOnInit

[英]How to call the ngOnInit of component from another component

I want to call the ngonInit() of a component from another component, so I use the BehaviorSubject to do that.我想从另一个组件调用一个组件的 ngonInit(),所以我使用 BehaviorSubject 来做到这一点。

I have a component A which is a nav bar component so it is called at the beginning of the application and another component B. I want to reload the ngOnInit of the component A everytime I execute a function in the component B.我有一个组件 A,它是一个导航栏组件,因此它在应用程序的开头被调用,另一个组件 B 被调用。我想每次在组件 B 中执行 function 时重新加载组件 A 的 ngOnInit。

This is the component A:这是组件 A:

export class A implements OnInit {
 isDisabled : boolean ;
 client : any;
constructor(private draftDataService : DraftDataService) {
  this.draftDataService.getClient().subscribe( data =>  {this.client=data ; 
  })
 }
ngOnInit() {
  
      if(this.client)
      {  
        this.isDisabled = false;
      }
        else {
          this.isDisabled=true;
        }
      }}

And this is the component B:这是组件 B:

 export class B implements OnInit, OnDestroy {
   generalInfomodel : GeneralInfoModel= new GeneralInfoModel();
   public selectedClient: ItemClientModel;
   constructor(private draftDataService : DraftDataService){}
   ngOnInit() {}
   selectClient(e: any) {
     if (e) {
         this.selectedClient = e ;
         this.generalInfomodel.client = this.selectedClient;
         this.draftDataService.sendClient(this.generalInfomodel.client);
      }}
       }

And this is the service:这就是服务:

export class DraftDataService {
 constructor() { }
  private subjectClient = new BehaviorSubject<any>('');
  sendClient( client : ItemClientModel){
     this.subjectClient.next(client);
   }
  getClient() : Observable<ItemClientModel>{
    return this.subjectClient.asObservable();
   }}

The problem is that component A is loaded in the beginning so it receive an empty string 'Client' and when the function 'selectClient' in the component B is executed the component A didn't receive it I don't know why?问题是组件A在开始时被加载,所以它收到一个空字符串'Client',当组件B中的function'selectClient'被执行时,组件A没有收到它我不知道为什么? I mean when I console the client in the component A it is showed only one time with undefined value!我的意思是,当我在组件 A 中控制台客户端时,它只显示一次,但值未定义! Can anyone help me to resolve this issue ?谁能帮我解决这个问题?

The short answer is to use ViewChild to call the function on component B.简短的回答是使用ViewChild调用组件 B 上的 function。

In your HTML Template give ComponentB a name:在您的 HTML 模板中为 ComponentB 命名:

<app-componentb #componentb></app-componentb>

In the ComponentB's parent, do something like this:在 ComponentB 的父级中,执行以下操作:

@ViewChild(componentB) componentB: ComponentB

And then in ComponentB's parent you can do this:然后在 ComponentB 的父级中,您可以这样做:

componentB.ngOnInit()

Generally I would not call Angular component hierarchy methods like this unless it was part of a unit test.通常我不会像这样调用 Angular 组件层次结构方法,除非它是单元测试的一部分。 I'd put that functionality into another method that is called from both ngOnInit() and external methods.我将该功能放入从 ngOnInit() 和外部方法调用的另一个方法中。

If ComponentA is not aa parent of ComponentB things are slightly more difficult.如果 ComponentA 不是 ComponentB 的父级,事情会稍微困难一些。

If they are siblings on the same parent, you could dispatch an event from ComponentA to tell the parent to reset ComponentB.如果它们是同一个父级的兄弟姐妹,您可以从 ComponentA 分派一个事件来告诉父级重置 ComponentB。

Or if you have a different architecture, you could put an Observable in a service that is shared in both ComponentA and ComponentB.或者,如果您有不同的架构,您可以将 Observable 放在一个服务中,该服务在 ComponentA 和 ComponentB 中共享。 ComponentA will trigger a next() and ComponentB will subscribe to force the change. ComponentA 将触发next()并且 ComponentB 将订阅以强制更改。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM