私は、要素の幅を式としてテンプレートに公開するNericディレクティブです。後で問題を再現するときにこれを再利用することができます。
@Directive({
selector: 'on-width'
})
export class OnWidthDirective implements DoCheck {
@Output('on-width')
public widths: EventEmitter<number> = new EventEmitter();
private _lastWidth: number;
public constructor(private el: ElementRef) {}
public ngDoCheck(): void {
const w = this.el.nativeElement.getBoundingClientRect().width;
if(this._lastWidth !== w) {
this.widths.next(w);
this._lastWidth = w;
}
}
}
OutterComponentのテンプレートでは、どのテンプレート要素でも幅の変化を聴くことができます。
@Component({
template: '<child-thing (on-width)="onWidth($event)"></child-thing>'
})
export class OuterComponent {
public onWidth(width: number) {
console.log(width); // called when child resizes
}
}
https://angular.io/guide/component-interaction –