Angular 2 从一个组件到下一个组件传递事件

Angular 2 pass event from one component to the next

父组件我有一个客户列表,我正在传递客户 ID。

<a (click)="onClient($event, ID)">edit</a> View

export class ListComponents
 public myformdata: number;
  onClient(ID: number) {

       console.log('loading ID', JSON.stringify(ID));

       this.myformdata = (ID);
       return this.myformdata;
    }
}`

在子构造函数上我可以绑定它。但是每次单击子构造函数时如何将值注入。我尝试了一个生命周期钩子

ngOnChanges(): void {

}
export class ChldComponents{
    constructor(public _dashlit: ListComponents) {
}
}

我不能 100% 确定您要完成的任务。如果您只是想将 myformdata 传递给 childComponent,您可以在单击父组件时设置它

parent.component.ts

onClient(ID: number){
    this.myformdata = ID;
}

然后使用输入指令将其传递到您的子组件中

child.component.html

<child-component [myInput]="myformdata"></child-component>

现在它可以在您的子组件中使用,并且会随着父组件的变化而变化 this.myformdata。如果您需要在输入更改时 运行 某些函数,您可以使用 OnChanges 生命周期挂钩。

https://angular.io/docs/ts/latest/cookbook/component-communication.html https://angular.io/docs/ts/latest/api/core/index/OnChanges-class.html