解决方法
HostListener
一种方法是使用HostListener装饰器.当host元素发出指定的事件时,将调用trim方法.
@HostListener('window:resize',['$event']) onResize(event) { this.width = event.target.innerWidth; this.height = event.target.innerHeight; }
通过ngZone
另一种方法是将ngZone导入组件.然后,您可以使用NgZone检查onresize事件.
constructor(ngZone:NgZone) { window.onresize = (e) => { ngZone.run(() => { this.width = window.innerWidth; this.height = window.innerHeight; }); }; }