angular中如何获取被引用组件内的方法

步骤:

  1. 设置一个变量,将其用 ViewChild装饰,
  2. 将该变量的类型设定为被引用组件的类名(class 后面的那个单词),如注释B中所释代码
  3. 调用该变量,采用变量名+“." 的方式调用该变量内的方法,如注释C中所释代码。
import {Component, OnInit, ViewChild} from "@angular/core";
import {FormComponent} from "../../../../core/_component/form.component";
import {EntityFormComponent} from "../../../../core";

@Component({
    selector: 'app-set-password',
    template: `
        <div #passWordForm
             class="tab-pane app-entity-form" <!--A:app-entity-form为被引用的组件-->
         >
        </div>
        <ng-template #pwdBtnGroupTemplate>
             <button type="button" class="btn btn-primary"
                 (click)="$onSubmitPwd($event);">保存
             </button>
        </ng-template>
    `
})
export class SetPasswordComponent extends FormComponent implements OnInit{


    @ViewChild('pwdBtnGroupTemplate')
    pwdBtnGroup: TemplateRef<any>;

    @ViewChild('passWordForm')
    passWordForm: EntityFormComponent;//B:将该被引用组件定义为一个变量

    constructor() {

    }

    ngOnInit() {
    }

    $onSubmitPwd($event) {
        this.passWordForm && this.passWordForm.$onSubmit($event); // C:调取被引用组件内的方法
    }
}

//被引用组件

export class EntityFormComponent extends FormComponent implements OnInit, OnChanges {

     constructor() {

    }


    $onSubmit($event?) {
      console.log($event);
    }

    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值