我正在尝试使用 typescript 在 ionic 2 中获取设备宽度和高度。
在我使用的之前版本的ionic 2中,
window.screen.width
window.screen.height
但在较新的版本中,这不起作用。
它如何与 Type Script + ionic 2 一起使用?
最佳答案
您可以为此使用平台信息。
平台
内部使用window.innerWidth
和window.innerHeight
但是
Using this method is preferred since the dimension is a cached value, which reduces the chance of multiple and expensive DOM reads.
ionic 4/5
文档: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());
});
}
}
ionic 2/3
文档: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());
});
}
}
关于angular - 如何使用 TypeScript 在 Ionic 中获取设备宽度和高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38737341/