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);
    }

    
}

 

Angular 9,要实现父组件调用组件方法,可以通过以下步骤进行操作: 1. 在子组件定义一个公共方法,供父组件调用。可以在子组件的.ts文件添加一个greeting方法,如引用\[1\]和引用\[2\]所示。 2. 在父组件的模板文件,使用子组件的选择器来引入子组件。例如,如果子组件的选择器是`app-child`,则可以在父组件的模板文件使用`<app-child></app-child>`来引入子组件。 3. 在父组件的.ts文件,使用`@ViewChild`装饰器来获取组件的实例。在父组件的类添加一个ViewChild装饰器,如下所示: ```typescript import { Component, ViewChild } from '@angular/core'; import { ChildComponent } from './child.component'; @Component({ selector: 'app-parent', templateUrl: './parent.component.html', styleUrls: \['./parent.component.css'\] }) export class ParentComponent { @ViewChild(ChildComponent) childComponent: ChildComponent; callChildMethod() { this.childComponent.greeting('John'); } } ``` 在上面的代码,我们使用`@ViewChild`装饰器来获取组件的实例,并将其赋值给`childComponent`属性。 4. 在父组件的模板文件,可以通过调用组件方法来触发子组件方法。例如,可以在父组件的模板文件添加一个按钮,并在按钮的点击事件调用组件方法,如下所示: ```html <button (click)="callChildMethod()">调用组件方法</button> ``` 当点击按钮时,将会调用组件的`callChildMethod`方法,从而触发子组件的`greeting`方法,并传递参数。 这样,就实现了在Angular 9组件调用组件方法的功能。 #### 引用[.reference_title] - *1* [Angular组件——父组件调用组件方法](https://blog.csdn.net/weixin_33938733/article/details/86259262)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [angular-在父组件调用组件方法](https://blog.csdn.net/weixin_44726310/article/details/103361153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [angular4组件如何调用组件方法](https://blog.csdn.net/u012396955/article/details/78852140)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值