我正在开发一款离子游戏,如何通过父组件(应用板)中的方法获取子组件(应用正方形)的属性。应用板上有30多个应用方形组件,因此我希望它能够动态访问它board-component.html(parent)
<app-square (click)="makeMove(l1)" [count]="3" #l1></app-square>
<app-square #l2></app-square>
board-component.ts
makeMove(obj: SquareComponent){
//I want to change the value of l2 here
}
我知道我无法使用ViewChild,因为它在方法内部不起作用。
使用这样的板子组件内部的ViewChildren
装饰器对其进行引用
@ViewChildren(QuareComponent)
squareComp:QueryList<SquareComponent>;
然后在您的方法内部使用它
makeMove(){
console.log(this.squareComp.toArray()) //here are all your square components
}