简体   繁体   English

如何从 angular 中的父组件触发子组件的 function?

[英]How to trigger a function of child component from parent in angular?

I have a function component as follows:我有一个 function 组件如下:

export class ChildComp {
   whoAmI() {
     return 'I am a child!!';
   }
}

My parent component:我的父组件:

import { ChildComp  } form './child.component';
export class ParentComp {
   constructor(private child: childComp  ) {}
   triggerChildFunction() {
      this.childComp.whoAmI();
   }
 }

The above method did not work for me.上述方法对我不起作用。 Can anyone please suggest me help?有人可以建议我帮忙吗?

I think, that you child should be Angular service, not just class.我认为,您的孩子应该是 Angular 服务,而不仅仅是 class。

Injectable()
export class ChildService { // remember add this to module
   public whoAmI() {
     return 'I am a child!!';
   }
}

import { ChildService  } form './child.service';
export class ParentComp {
   constructor(private child: childService  ) {}
   triggerChildFunction() {
      this.childService.whoAmI();
   }
 }

You can also communicate two Angular components with Subject(), or use @ViewChild().您还可以使用 Subject() 与两个 Angular 组件通信,或使用 @ViewChild()。 More informaction about @ViewChild you can find here有关@ViewChild 的更多信息,您可以在此处找到

I guess it's the purpose of the concept of "Service".我想这是“服务”概念的目的。

my-service.service.ts我的服务.service.ts

@Injectable()
export class MyService<T> {
  public stream$ = new Subject<T>();

  public getSteam$() {
    return this.stream$;
  }

  public publish(value: T) {
    this.stream$.next(value);
  }

}

child.component.ts child.component.ts

@Component()
export class ChildComponent<T> implements OnInit, OnDestroy {
  public whoami = 'child';

  private subscription: Subscription;

  constructor(
    private myService: MyService
  ) {}

  public ngOnInit() {
    this.subscription = this.myService.getStream$()
      .subscribe((value: T) => {
          this.functionToTrigger(value);
      });
  }

  public ngOnDestroy() {
    if(this.subscription) this.subscription.unsubscribe();
  }

  private functionToTrigger(arg: T) {
    // do your stuff
    console.log(JSON.stringify(arg))
  }
}

parent.component.ts父组件.ts

@Component()
export class ParentComponent<T> {
  public whoami = 'parent';

  constructor(
    private myService: MyService<T>
  ) {}

  public notifiyChild(value: T) {
    this.myService.publish(value);
  }
}

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

相关问题 从模板中的父组件触发子函数 - trigger child function from parent component in stencil 父组件中的触发事件来自路由Angular中子组件的加载 - Trigger event in Parent component from child component load in routing Angular Angular - 如何从父组件到子组件调用 function - Angular - How to invoke function from parent to child component 如何使用模板表单从Angular 2的父组件中触发子组件验证器 - How to trigger child component validators from parent component in Angular 2 using template forms 如何使用Vuejs 3中的render函数从子组件触发父组件上的方法 - How to trigger the method on parent component from child using render function in Vuejs 3 加载父组件数据后,如何从子组件调用函数(Angular 5) - How to call function from child component when parent component data has loaded (Angular 5) 如何从子组件调用父 function - How to invoke parent function from child component 如何从 Angular 9 中的子组件访问父组件 - How to Access Parent Component from Child Component in Angular 9 Angular 2,如何将选项从父组件传递到子组件? - Angular 2, How to pass options from parent component to child component? 角路由器如何从父组件调用子组件 - angular router how to call child component from the parent component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM