如何解决检测Angular 4中的实时窗口大小变化?
在初始化时得到它
public innerWidth: any;
ngOnInit() {
this.innerWidth = window.innerWidth;
}
如果您想在调整大小时保持更新:
@HostListener('window:resize', ['$event'])
onResize(event) {
this.innerWidth = window.innerWidth;
}
解决方法
我一直在尝试构建一个响应式导航栏并且不希望使用媒体查询,所以我打算使用*ngIf
窗口大小作为标准。但是我一直面临一个问题,因为我找不到任何关于
Angular 4 窗口大小检测的方法或文档。我也尝试过 JavaScript 方法,但不支持。
我还尝试了以下方法:
constructor(platform: Platform) {
platform.ready().then((readySource) => {
console.log('Width: ' + platform.width());
console.log('Height: ' + platform.height());
});
}
…用于离子。
并且screen.availHeight
,但仍然没有成功。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。