我很想将nativeElement
中的以下ngAfterViewInit
处理重构为静态方法的Typescript类:
export class AnimationBuilderComponent implements AfterViewInit {
@ViewChild('photos') photosContainer: ElementRef;
constructor(private animationBuilder: AnimationBuilder) {}
ngAfterViewInit(): void {
const div = this.photosContainer.nativeElement as HTMLDivElement;
if (!div) {
console.warn('the expected div element is not here');
return;
}
const children = Array.from(div.children);
if (!children) {
console.warn('the expected div element children are not here');
return;
}
if (!children.length) {
console.warn(
'the expected number of div element children is not here'
);
return;
}
children.forEach(el => {
if (el.nodeName !== 'img'.toUpperCase()) {
return;
}
console.log(el.clientWidth);
});
}
animate(): void {
console.log('animate!');
}
}
在我匆匆忙忙执行此操作之前,是否已经有一个NPM软件包已执行此操作?实际上,我正在寻找一种能感知ElementRef
的jQuery。