如何从另一个组件中的一个组件调用函数?

时间:2018-08-31 09:15:16

标签: angular typescript ionic-framework ionic3

我的问题是我有两个组件(让它们称为 comp1 comp2 ),我必须在 comp1中调用函数show_it(list) comp2 中的一些数据。因此,目前,如果我使用按钮轻松触发它,该按钮将在 comp2 中实现。但是我的目的是,我想通过单击带有(click)事件的标签来触发 comp1 中的 comp2 功能。有人可以解决我的问题吗?

我如何在home.html中调用comp1和comp2:

 <comp1 [list]="lists" (emitList)="onListUpdate($event)"></comp1>
 <comp2 [list]="additionalInformationList"></comp2>

我的home.ts的onListUpdate功能:

onListUpdate(list: {item:string}[]){
    this.additionalInformationList=list;
    console.log(list);
}

我的comp1的.html:

div>
<ion-list>
    <ion-item *ngFor="let list of list">
        <ion-label (click)="updateList(list)">{{list.item.free_text}}</ion-label>
        <ion-checkbox *ngIf="list.item.checkbox==true"  disabled="false"></ion-checkbox>
    </ion-item>
</ion-list>

我的comp1的.ts:

@Component({
  selector: 'caiditems',
  templateUrl: 'caiditems.html'
})
export class CaiditemsComponent {
  transformedList=[];

  @Input()
  list = []; 

  @Output() 
  emitList = new EventEmitter<{item:string}[]>();

  constructor() {
    this.emitList.emit(this.list);
  }

  updateList(list) {
     if(list.item.additional_information!=null){
       this.transformedList=[
        {item: {id: list.item.id, free_text: list.item.free_text, checkbox: list.item.checkbox, additional_information: list.item.additional_information}}]
      console.log("not empty")
     }
     else{
       this.transformedList=[{item: {id: list.item.id, free_text: list.item.free_text, checkbox: list.item.checkbox, additional_information: ""}}]
       console.log("empty")
     }
     this.emitAdditionalinformations(this.transformedList);
     console.log('additional information list updated')
  }

  emitAdditionalinformations(transformedList){
    this.emitList.emit(this.transformedList);
    console.log('list emited');
  }

我的comp2的.html:

<div>
  <button ion-button (click)="show_it(list)">show it to meeeeeee</button><br>
  <ion-label>{{additionalinformation}}<br><br></ion-label>
</div>

我的comp2档案:

@Component({
  selector: 'caidadditionalinformations',
  templateUrl: 'caidadditionalinformations.html'
})
export class CaidadditionalinformationsComponent {
  additionalinformation="hi";

@Input()
list = []; 

constructor() {  
}

show_it(list){
  console.log(list);
  if(list[0].item.additional_information!=""){
      console.log(list[0].item.additional_information);
      this.additionalinformation=list[0].item.additional_information;
   }
  else{
      this.additionalinformation="no additional informations";
   }
}

1 个答案:

答案 0 :(得分:1)

从这段代码中摘录:

 <comp1 [list]="lists" (emitList)="onListUpdate($event)"></comp1>
 <comp2 [list]="additionalInformationList"></comp2>

我假设comp1comp2都是HomePage的子代。当您从comp1单击标签时,onListUpdate($event)的方法在HomePage中执行,对吗?

您可以在comp2中获得HomePage组件的引用,然后使用该引用调用show_it()方法。

会是这样的:

@Component(...)
export class HomePage {

  // Get a reference of the component instance
  @ViewChild(CaidadditionalinformationsComponent) comp2: CaidadditionalinformationsComponent;

  onListUpdate(list: {item:string}[]){
    this.additionalInformationList = list;
    console.log(list);

    // Use the reference to call the public method
    this.comp2.show_it(list);
  }

} 

如果在您的应用程序上下文中这没有意义,请告诉我,也许创建一个stackblitz项目将有助于我们更好地了解您的特定情况。

还有一些在组件之间共享数据的方法:

  1. 使用共享服务,您可以在其中保存数据,并且两个组件都可以从中读取数据
  2. 使用Ionic events,以便两个组件都可以发布/订阅数据更改,而无需Homepage作为中介。