angular 中父组件调用子组件的方法

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

猜你喜欢

转载自www.cnblogs.com/monkey-K/p/11567123.html