Angular2来自另一个组件的调用函数

时间:2016-09-01 10:29:20

标签: javascript angular typescript eventemitter

我有两个组件:NgbdAlertCloseable和AlertCtrl。我还有AppComponent作为父组件。我想要的是单击AlertCtrl组件中的按钮并在NgdbAlertCloseable组件上创建警报。

addSuccess()函数向视图添加一个警报,当我在其组件内部调用它时,它运行良好。但是,我尝试使用EventEmitter从另一个组件调用此函数(如此处所建议:https://stackoverflow.com/a/37587862/5291422)但它会出现此错误:

ORIGINAL EXCEPTION: TypeError: self._NgbdAlertCloseable_2_4.addSuccess is not a function

以下是我的文件:

ngbd警报-closeable.component.ts

import { Input, Component } from '@angular/core';

@Component({
  selector: 'ngbd-alert-closeable',
  templateUrl: './app/alert-closeable.html'
})
export class NgbdAlertCloseable {

  @Input()
  public alerts: Array<IAlert> = [];

  private backup: Array<IAlert>;

  private index: number; 

  constructor() {
    this.index = 1;
  }

  public closeAlert(alert: IAlert) {
    const index: number = this.alerts.indexOf(alert);
    this.alerts.splice(index, 1);
  }

  public static addSuccess(alert: IAlert) {
    this.alerts.push({
      id: this.index,
      type: 'success',
      message: 'This is an success alert',
    });
    this.index += 1;
  }

  public addInfo(alert: IAlert) {
    this.alerts.push({
      id: this.index,
      type: 'info',
      message: 'This is an info alert',
    });
    this.index += 1;
  }

}

interface IAlert {
  id: number;
  type: string;
  message: string;
}

警报-ctrl.component.ts

import { EventEmitter, Output, Component } from '@angular/core';
import { NgbdAlertCloseable } from './ngbd-alert-closeable.component';

@Component({
  selector: 'alert-ctrl',
  template: '<button class="btn btn-success" (click)="addSuccessMsg()">Add</button>'
})

export class AlertCtrl {
    @Output() msgEvent = new EventEmitter(); 
    public addSuccessMsg(){
        this.msgEvent.emit(null);
    }
}

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: '<div class="col-sm-4"><alert-ctrl (msgEvent)="ngbdalertcloseable.addSuccess()"></alert-ctrl><ngbd-alert-closeable #ngbdalertcloseable></ngbd-alert-closeable>'
})

export class AppComponent { }

我使用它错了吗?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

检查addSuccess函数是否为静态且使用非静态属性。

应该是:

 public addSuccess(alert: IAlert) {
    this.alerts.push({
      id: this.index,
      type: 'success',
      message: 'This is an success alert',
    });
    this.index += 1;
 }

在您的视图中,您必须在此示例中传递IAlert值,当我们调用msgEvent.emit(IAlert)时,我们将发送该值。

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: '<div class="col-sm-4"><alert-ctrl (msgEvent)="ngbdalertcloseable.addSuccess($event)"></alert-ctrl><ngbd-alert-closeable #ngbdalertcloseable></ngbd-alert-closeable>'
})

export class AppComponent { }

然后你必须发送IAlert,我只是为了演示目的而改变你的AlertCtrl。

import { EventEmitter, Output, Component } from '@angular/core';
import { NgbdAlertCloseable } from './ngbd-alert-closeable.component';

@Component({
  selector: 'alert-ctrl',
  template: '<button class="btn btn-success" (click)="addSuccessMsg()">Add</button>'
})

export class AlertCtrl {

    currentAlert:IAlert = {id: 0, type: 'success', message: 'This is an success alert'};

    @Output() msgEvent = new EventEmitter<IAlert>(); 
    public addSuccessMsg(){
        this.msgEvent.emit(this.currentAlert);
    }
}

祝你好运,编码愉快!