50

我正在尝试使用打字稿在 ionic 2 中获取设备宽度和高度。

在我使用的以前版本的 ionic 2 中,

window.screen.width
window.screen.height

但在较新的版本中,这不起作用。

Type Script + ionic 2 是如何工作的?

4

2 回答 2

111

您可以为此使用平台信息。

在内部platform使用window.innerWidthand window.innerHeightbut

使用这种方法是首选,因为维度是一个缓存值,这减少了多次和昂贵的 DOM 读取的机会。

离子 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());
    });
  }
}

离子 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());
    });
  }
}
于 2016-08-03T10:23:09.010 回答
7

尝试使用 window.innerHeight 和 window.innerWidth 分别获取设备的高度和宽度。

于 2016-08-03T08:36:24.623 回答