【问题标题】:How to call the method from child component to parent component in Angular如何在Angular中将方法从子组件调用到父组件
【发布时间】:2021-10-27 22:06:43
【问题描述】:

我在子组件中有一种方法,如下所示:

showAcknowledgement() {
    this.noticeService.getNotice(ConstUrls.NoticeManagement.GetUserNotice).subscribe((data: any) => {
      data.data.filter((res: any) => {
      if (data.isSuccessful === true) {
        this.noticeText = res.noticeText;
        this.noticeAttachment = res.noticeAttachement;
        this.noticeCode = res.noticeCode;
      } else if (data.isSuccessful === false) {
        this.message = [{ field: "", message: data.message[0].message }];
        this.objError = this.message;
        this.showError = true;
        window.scrollTo({ top: 0, behavior: 'smooth' });
      }
    })
    });
  }

我需要在 ngOnInit() 内的父组件中调用此方法,因为在页面加载时我需要调用该方法。我尝试如下,但它不起作用

父 HTML:

<app-acknowledge-notice-modal #child></app-acknowledge-notice-modal>

父 TS:

@ViewChild(AcknowledgeNoticeModalComponent, {static : true}) child : AcknowledgeNoticeModalComponent;


ngOnInit() {
    this.child.showAcknowledgement();
}

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    在子组件内带有@Output() (https://angular.io/guide/inputs-outputs)

    这不是直接的方法调用,而是很常见的事情。所以,在你的情况下,它可能是这样的:

    父 HTML:

    <app-acknowledge-notice-modal (someEvent)=someMethod()></app-acknowledge-notice-modal>
    

    儿童 TS:

    @Output() someEvent = new EventEmitter<string>();
    

    父 HTML 中的someEvent 必须与子 TS 匹配,而 someMethod() 是父 TS 中的方法

    【讨论】:

    • 我已经尝试过这种方式,但无法实现
    • “无法实现”是什么意思?是否存在技术限制、错误等?
    【解决方案2】:

    你可以这样试试吗?

    使用类型选择器:

    子组件

    @Component({
      selector: 'app-acknowledge-notice-modal',
      template: <div>...</div>
    })
    
    class AcknowledgeNoticeModalComponent{
    showAcknowledgement() {
    this.noticeService.getNotice(ConstUrls.NoticeManagement.GetUserNotice).subscribe((data: any) => {
          data.data.filter((res: any) => {
          if (data.isSuccessful === true) {
            this.noticeText = res.noticeText;
            this.noticeAttachment = res.noticeAttachement;
            this.noticeCode = res.noticeCode;
          } else if (data.isSuccessful === false) {
            this.message = [{ field: "", message: data.message[0].message }];
            this.objError = this.message;
            this.showError = true;
            window.scrollTo({ top: 0, behavior: 'smooth' });
          }
         })
        });
       }
     }
    

    父组件

    @Component({
      selector: parent-component,
      template: <app-acknowledge-notice-modal></app-acknowledge-notice-modal>,
      directives: [AcknowledgeNoticeModalComponent]
    })
    
    class ParentComponent{
    
      @ViewChild(AcknowledgeNoticeModalComponent) child: AcknowledgeNoticeModalComponent;
    
      ngAfterViewInit() {        
        this.child.showAcknowledgement();
      }
    }
    

    使用字符串选择器:

    子组件

    @Component({
      selector: 'app-acknowledge-notice-modal',
      template: <div>...</div>
    })
    
    class AcknowledgeNoticeModalComponent{
    showAcknowledgement() {
    this.noticeService.getNotice(ConstUrls.NoticeManagement.GetUserNotice).subscribe((data: any) => {
          data.data.filter((res: any) => {
          if (data.isSuccessful === true) {
            this.noticeText = res.noticeText;
            this.noticeAttachment = res.noticeAttachement;
            this.noticeCode = res.noticeCode;
          } else if (data.isSuccessful === false) {
            this.message = [{ field: "", message: data.message[0].message }];
            this.objError = this.message;
            this.showError = true;
            window.scrollTo({ top: 0, behavior: 'smooth' });
          }
         })
        });
       }
     }
    

    父组件

    @Component({
      selector: parent-component,
      template: <app-acknowledge-notice-modal #child></app-acknowledge-notice-modal>,
      directives: [AcknowledgeNoticeModalComponent]
    })
    
    class ParentComponent{
    
      @ViewChild('child') child: AcknowledgeNoticeModalComponent;
    
      ngAfterViewInit() {        
        this.child.showAcknowledgement();
      }
    }
    

    【讨论】:

    • 在我的父组件中,我也有菜单和加载器代码,所以我不能使用字符串选择器
    • @SalahuddinAhmed 我也在等着找出 OP 结果!你有一个有据可查的方法,它也是我在 angular 文档中信任的方法。
    • @Salahuddin Ahmed:我尝试使用类型选择器也不起作用。
    猜你喜欢
    • 2023-01-08
    • 2019-04-03
    • 1970-01-01
    • 2016-12-22
    • 2021-05-02
    • 2020-10-20
    • 2021-04-07
    相关资源
    最近更新 更多