如果尚未调用子组件,如何从父级调用子组件?

问题描述 投票:0回答:1

我有一个父组件和一个子组件,可以使用popum组件添加和更新图块,现在添加按钮恰好在父组件中,而更新位于子组件中。当没有图块(子组件标记)显示时,我必须添加一个图块,以至于我无法调用子组件的方法openAddOrUpdatePopup()。

我如何在不依赖子组件的情况下在父组件中调用此方法?

我的代码:-parent-html

<input type="button" class="btn" value="Add" (click)="applicationTile.openAddOrUpdatePopup()" />
<application-tile class="grid-item action-grid-item enable-events" *ngFor="let application of paginationParams.data" [ngClass]="{'active':activeApplication && activeApplication.applicationId == application.applicationId}" [application]="application"></application-tile>

parent.ts

export class ApplicationsComponent implements OnInit {
 @ViewChild (ApplicationTileComponent, {static: false}) applicationTile: ApplicationTileComponent ; 
}

Child.html

<label class="name">{{application.applicationName}}</label>
<div class="table-view">

<div class="table-view-row">
    <div>
        <label>Application value </label>
        <span>{{application.value ? application.value  : '-'}}</span>
    </div>
        <label>Application Version</label>
        <span>{{application.appVersion ? application.appVersion : '-'}}</span>
    </div>
    <ul class="actions">
        <li>
            <span class="icon svg-icon action-icon edit-icon" title="Edit" 
 (click)="openAddOrUpdatePopup(application, $event)"></span>
        </li>

    </ul>
</div>

<av-popup class="medium-popup" *ngIf="popupParams && popupParams.name == 'create-or-update-application'" [popupParams]="popupParams" (close)="popupParams = null">
<form name="form" (ngSubmit)="f.form.valid && popupParams.saveOrUpdateApplication()" #f="ngForm"></form></av-popup>

child.ts

 openAddOrUpdatePopup(application?, $event?) {
       if (application) {
        application = JSON.parse(JSON.stringify(application));
       }
       else {
        application = {
        applicationType: this.applicationTypes[0].applicationTypeVal
       }
     }

this.popupParams = {
  name: 'create-or-update-application',
  title: application.applicationId ? 'Edit ' + application.applicationName : "Create New Application",
  application: application
}

}

typescript components angular6 parent-child angular8
1个回答
0
投票

您能详细说明问题吗?或者您可以将工作代码发布到Stackblitz中并提供链接?

通过几种方式与父子组件连接,

输入和输出。

与观察者共享的服务(发布和订阅)

© www.soinside.com 2019 - 2024. All rights reserved.