angular中 父組件調用子組件的方法 -- 使用 @ViewChild 裝飾器修飾子組件,獲取方法,調用
除此之外 ViewChild 還可以獲取 DOM ,操作 DOM , 詳見: https://www.cnblogs.com/monkey-K/p/11567098.html
1. html 中使用 #var 方式標記 子組件
<div style="border: 1px solid red">
<p>子組件:</p>>
<app-login #login></app-login>
<button (click)="userChildMethod()">調用子組件方法</button>
</div>
2. js 中使用 @ViewChild 裝飾器
@ViewChild('login', null) loginComponent: any
userChildMethod() {
// 調用子組件方法
this.loginComponent.loginIn()
}
注: angular 中如果不知道 類型 就定義為 any