如何解决将参数映射到 ViewChild 元素的 Angular 工具
对于我构建的视频会议应用程序,我有一种方法(如下)需要重构。该应用程序运行良好,实际上有 18 个磁贴,但为了便于讨论,我将其缩写。 Angular 中有没有办法(在本例中为 Angular 10.0.6 w/TypeScript 3.9.5)使用动态参数访问 @ViewChild/ElementRef?
这是我使用的方法:
getTilesFromIndex = (index: number) => {
const tileObject = {
tile: null,video: null,nameplate: null
};
switch (index) {
case 0:
tileObject.tile = this.tile0.nativeElement;
tileObject.video = this.video0.nativeElement;
tileObject.nameplate = this.nameplate0.nativeElement;
break;
case 1:
tileObject.tile = this.tile1.nativeElement;
tileObject.video = this.video1.nativeElement;
tileObject.nameplate = this.nameplate1.nativeElement;
break;
case 2:
tileObject.tile = this.tile2.nativeElement;
tileObject.video = this.video2.nativeElement;
tileObject.nameplate = this.nameplate2.nativeElement;
break;
case 3:
tileObject.tile = this.tile3.nativeElement;
tileObject.video = this.video3.nativeElement;
tileObject.nameplate = this.nameplate3.nativeElement;
break;
case 4:
tileObject.tile = this.tile4.nativeElement;
tileObject.video = this.video4.nativeElement;
tileObject.nameplate = this.nameplate4.nativeElement;
break;
}
return tileObject;
}
它引用了相应的标记:
<div id="tile-0" style="display:none" #tile0>
<video id="video-0" class="w-100 h-100" #video0></video>
<div id="nameplate-0" #nameplate0></div>
</div>
<div id="tile-1" style="display:none" #tile1>
<video id="video-1" class="w-100 h-100" #video1></video>
<div id="nameplate-1" #nameplate1></div>
</div>
<div id="tile-2" style="display:none" #tile2>
<video id="video-2" class="w-100 h-100" #video2></video>
<div id="nameplate-2" #nameplate2></div>
</div>
<div id="tile-3" style="display:none" #tile3>
<video id="video-3" class="w-100 h-100" #video3></video>
<div id="nameplate-3" #nameplate3></div>
</div>
<div id="tile-4" style="display:none" #tile4>
<video id="video-4" class="w-100 h-100" #video4></video>
<div id="nameplate-4" #nameplate4></div>
</div>
@ViewChild 元素定义如下:
@ViewChild('tile0') tile0: ElementRef;
@ViewChild('tile1') tile1: ElementRef;
@ViewChild('tile2') tile2: ElementRef;
@ViewChild('tile3') tile3: ElementRef;
@ViewChild('tile4') tile4: ElementRef;
@ViewChild('video0') video0: ElementRef;
@ViewChild('video1') video1: ElementRef;
@ViewChild('video2') video2: ElementRef;
@ViewChild('video3') video3: ElementRef;
@ViewChild('video4') video4: ElementRef;
@ViewChild('nameplate0') nameplate0: ElementRef;
@ViewChild('nameplate1') nameplate1: ElementRef;
@ViewChild('nameplate2') nameplate2: ElementRef;
@ViewChild('nameplate3') nameplate3: ElementRef;
@ViewChild('nameplate4') nameplate4: ElementRef;
应用程序动态分配图块,该方法始终用于操作 DOM、停止和启动视频、切换可见性和更改布局。
我想摆脱这种对 ViewChild 元素的文字引用。我尝试过字符串文字,但只是对字符串进行操作。我将如何重构此方法,以便我可以采用 index 参数并返回对特定 @ViewChild 的引用。以伪代码为例:
getTilesFromIndex = (index: number) => {
const tileObject = {
tile: null,nameplate: null
};
tileObject.tile = this.tile${index}.nativeElement;
tileObject.video = this.video${index}.nativeElement;
tileObject.nameplate = this.nameplate${index}.nativeElement;
return tileObject;
}
我知道字符串文字不起作用,这样做只是为了传达我希望重构做什么的想法。我怎样才能让这个方法返回 viewChild 而不显式引用单个 @viewChild 的。
解决方法
这就是 Angular 中有 ViewChildrenin
的原因。但是它返回一个 QueryList
,因此您需要订阅它。还需要以某种方式对 HTML 元素进行分组(例如添加 CSS 类,或通过读取它们的类型)。
@ViewChildren('.video') videos!: QueryList<ElementRef>;
ngAfgterViewInit() {
this.videos.changes.subscribe(...
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。