Angular 当我在组件A中获取该函数的事件时,如何调用组件B中的函数

Angular 当我在组件A中获取该函数的事件时,如何调用组件B中的函数,angular,events,eventemitter,Angular,Events,Eventemitter,我想在从ws获取事件时使用事件发射器: 我有一个获取事件的函数,当我有一个事件时,我想调用其他组件中的函数 组件A,类型脚本代码: @Output() notify: EventEmitter<any> = new EventEmitter(); getSocketevent() { this.socket.on('data', (data) => { this.notify.emit(data) }); } 当我在组件A中获取此函数的事件时,如何调用组件B中

我想在从ws获取事件时使用事件发射器:

我有一个获取事件的函数,当我有一个事件时,我想调用其他组件中的函数

组件A,类型脚本代码:

@Output() notify: EventEmitter<any> = new EventEmitter();
getSocketevent() {
  this.socket.on('data', (data) => {
    this.notify.emit(data)
  });
}

当我在组件A中获取此函数的事件时,如何调用组件B中的函数?

您可以尝试从父级调用子方法:

<app-component-B #child (click)='getSocketevent()'></app-component-B> 

当父组件类需要这种访问时, 将子组件作为一个组件注入父组件中

在您的情况下,它将是:

// A component.ts
....
@ViewChild(BComponent, {static: false})
private bComponent: BComponent;

getSocketevent() {
  this.socket.on('data', (data) => {
    // this.notify.emit(data)
    this.bComponent.getprod();
  });
}


在这种情况下,您可以看到您实际上不需要使用
EventEmitter

发出
数据的目的是什么
孩子将如何使用它?我只想在从socketchild发出时调用函数getprod()?I声明<代码>子项:任何并在获取数据时调用,但是错误
core.js:9110错误类型错误:无法读取未定义的属性“getprod”
this.child在调用时未定义,在哪里调用它?我更改了它,但存在错误<类型上不存在代码>属性“child”如何声明child?是否将
#child
放在app-component-B标记上?在这种情况下,局部变量方法受到限制,因为父子关联必须完全在父模板内完成。在这种情况下,OP需要从父组件类(ts)调用子组件中定义的函数。
<app-component-B #child (click)='getSocketevent()'></app-component-B> 
getSocketevent() {
    var self = this;
    this.socket.on('data', (data) => {
    this.child.getprod(); // call getprod
      this.notify.emit(data)
    });
  }
// A component.ts
....
@ViewChild(BComponent, {static: false})
private bComponent: BComponent;

getSocketevent() {
  this.socket.on('data', (data) => {
    // this.notify.emit(data)
    this.bComponent.getprod();
  });
}