父子組件傳值的時候往往會用到 @Input和@Output 裝飾器,
@Input用於數據從父組件流入子組件,@Output用於從子組件傳出數據到父組件。
一. @Input
從 A組件(父組件):app-detail 給 B組件(子組件): app-detail-console 傳值:(1)在 A 組件的 html 中這樣寫:
(2)在 B 組件的 ts 中定義:
這樣就實現了將A組件中的 'zhizhanzhishang' 傳給了B組件中的參數 instanceId 和 refresh
二. @Output
從 B組件(子組件): app-detail-console 給 A組件(父組件):app-detail 傳值:(1)在 B 組件的 ts 中定義:
(2)在 A 組件的 html 中接收並使用:
在 A 組件的 ts 中打印出該值: