我在其他组件(父级)的页面html中有一个组件(子),如下所示:
< DIV> < child-component(focusEmit)=“startAnimation()”>< / child-component>< / DIV>在……
你可以使用Subject(rxJs) http://reactivex.io/rxjs/manual/overview.html#subject 这是EventEmitter的替代品。
通过@Output eventEmitter,可以这样做
子组件
@Output() focusEmit = new EventEmitter<any>(); focusFunction() { this.focusEmit.emit(true); }
父组件
HTML
<child-component (focusEmit)="startAnimation(event)"></child-component>
component.ts
startAnimation($event) { // Handle your code }
的 使用ViewChild模式 强>
<child-component #childAttr></child-component> @ViewChild("childAttr") childAttr; childAttrId : string; startAnimation() { this.childAttrId = childAttr.id; }
但您的孩子组件保持不变
focusFunction() { this.id = "focus triggered"; }