我正在尝试使用打字稿在 ionic 2 中获取设备宽度和高度。
在我使用的以前版本的 ionic 2 中,
window.screen.width
window.screen.height
但在较新的版本中,这不起作用。
Type Script + ionic 2 是如何工作的?
我正在尝试使用打字稿在 ionic 2 中获取设备宽度和高度。
在我使用的以前版本的 ionic 2 中,
window.screen.width
window.screen.height
但在较新的版本中,这不起作用。
Type Script + ionic 2 是如何工作的?
您可以为此使用平台信息。
在内部platform
使用window.innerWidth
and window.innerHeight
but
使用这种方法是首选,因为维度是一个缓存值,这减少了多次和昂贵的 DOM 读取的机会。
文档:https ://ionicframework.com/docs/angular/platform#width-number
import { Component } from '@angular/core';
import { Platform } from '@ionic/angular';
@Component({...})
export MyApp {
constructor(platform: Platform) {
platform.ready().then(() => {
console.log('Width: ' + platform.width());
console.log('Height: ' + platform.height());
});
}
}
文档:https ://ionicframework.com/docs/v3/api/platform/Platform/#width
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
@Component({...})
export MyApp {
constructor(platform: Platform) {
platform.ready().then(() => {
console.log('Width: ' + platform.width());
console.log('Height: ' + platform.height());
});
}
}
尝试使用 window.innerHeight 和 window.innerWidth 分别获取设备的高度和宽度。