1. 通过@ViewChild获取子组件,得到子组件的值、调用子组件的方法
//子组件child
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.scss']
})
export class Child{
public content:string = '123'
public change(){
this.content = 'aa'
}
}
//父组件parent
<app-child #Children></app-child>
import { ViewChild } from '@angular/core';
@Component({
selector: 'app-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.scss']
})
export class Parent{
//通过@ViewChild获取子组件,得到子组件的值、调用子组件的方法
@ViewChild('Children', { static: true }) children: any;
public show():void{
//获取子组件的属性以及方法
console.log(this.children.content)
this.children.change();
}
}
2.通过@ViewChild获取某个元素