例子:product-list為parent Component,product-alert為child Component.
(1) child Component裡必需的開發
從@
angular /core裡導入Input, Output和EventEmitter:
建立一個名為product的屬性,用于從parent Component裡接收目前選中的product.
建立一個EventEmitter執行個體,用于向parent Component發送事件。
在product alert模闆裡,給按鈕點選click事件附上處理函數:notify.emit()
<p *ngIf="product.price > 700">
<button (click)="notify.emit()">Notify Me</button>
</p>
(2) parent Component裡必需的開發
使用文法中括号[]給child Component的Input屬性指派,使用文法大括号{}給child Component的Output屬性指派:
onNotify的實作,位于parent Component ts檔案内:
最後的效果: