angular - 如何使用 TypeScript 在 Ionic 中获取设备宽度和高度?

标签 angular typescript ionic-framework ionic2 ionic3

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

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

window.screen.width
window.screen.height

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

它如何与 Type Script + ionic 2 一起使用?

最佳答案

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

平台内部使用window.innerWidthwindow.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/

相关文章:

angular - 如何导入其他模块组件可以识别的一次性全局模块

新选项卡上的 Angular 6 routerLink

reactjs - CombineReducers with Typescript 返回错误 "Argument of type is not assignable to parameter of type ' ReducersMapObject'"

javascript - 我如何访问同一工厂的不同 javascript 函数中的变量?

javascript - 是否可以在 angular-cli 中更改 `ng serve` 配置(我想添加一些规则,因此某些请求将被重定向到另一个端口)?

Angular 2 路由器链接问题

javascript - 在 Typescript Vue 项目中使用 Javascript 模块

javascript - 使用 React 和 Typescript 创建可重用的按钮组件,但出现不可分配类型错误

angularjs - 与 Ionic Apps 一起使用的最佳富文本编辑器是什么?

php - 如何使用ionic 2向mysql数据库插入数据?我尝试了很多解决方案,但结果仍然相同