繁体   English   中英

使用EventEmitter在两个组件之间进行Angular2通信

[英]Angular2 Communication between two components with EventEmitter

我有两个组件: AppCompSimulationComp

AppComp包含一个功能:

generateEmptyPromise() {
   return Promise.resolved('')
}

并具有以下html:

<simulation-comp (simu)='generateEmptyPromise()'></simulation-comp>

Simulation comp像这样处理(simu)

@Output() simu = new EventEmitter()
private eventHandled: boolean = false

// Triggered when a button of the component is pressed
whenClicked() {
  this.simu.subscribe(() => {
     this.eventHandled= true
  })
  this.simu.emit()
}

我想要的是基于generateEmptyPromise给出的承诺使eventHandled变为true(因此,在处理完发射之后)。 但是,它在atm上不起作用,我该如何调整我的代码以实现这种行为? 也许它不应该这样工作,而我在这里完全错了。

@Output用于将组件“外部”的值发送回父级。 子->父。

对于@Input > Child Communication,其中父模板包含要与之通信的子对象,您需要@Input以便子对象知道父节点将在某个时间点发送这些值。

因此,开始时可能需要appCmp

<simulation-comp [myinput]='generateEmptyPromise()'></simulation-comp>

SimulationCmp.ts

@Input myinput : any //Whatever value type you expect it to be

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM