我找到了这个解决方案.有效吗?

import {Component} from '@angular/core';
import {Platform} from 'ionic-angular';

@Component({...})
export MyApp {
constructor(platform: Platform) {
  platform.ready().then((readySource) => {
    console.log('Width: ' + platform.width());
    console.log('Height: ' + platform.height());
  });
 }
}

该溶液适用于ionic 2.

请告诉我正确的方法.

推荐答案

我找到了解决办法.答案很简单.在构造函数中编写以下代码.

import { Component, OnInit, OnDestroy, Input } from "@angular/core";
// Import this, and write at the top of your .ts file
import { HostListener } from "@angular/core";

@Component({
  selector: "app-login",
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})

export class LoginComponent implements OnInit, OnDestroy {
    // Declare height and width variables
    scrHeight:any;
    scrWidth:any;

    @HostListener('window:resize', ['$event'])
    getScreenSize(event?) {
          this.scrHeight = window.innerHeight;
          this.scrWidth = window.innerWidth;
          console.log(this.scrHeight, this.scrWidth);
    }

    // Constructor
    constructor() {
        this.getScreenSize();
    }


}

====== Working Code (Another) ======

export class Dashboard  {
 mobHeight: any;
 mobWidth: any;
     constructor(private router:Router, private http: Http){
        this.mobHeight = (window.screen.height) + "px";
        this.mobWidth = (window.screen.width) + "px";
          console.log(this.mobHeight);
          console.log(this.mobWidth)
     }
}

Typescript相关问答推荐

如何在不使用变量的情况下静态判断运算式的类型?

忽略和K的定义

HttpClient中的文本响应类型选项使用什么编码?''

打印脚本丢失函数的泛型上下文

如何在已知基类的任何子类上使用`extends`?

为什么ESLint会抱怨函数调用返回的隐式`any`?

Angular 自定义验证控件未获得表单值

返回嵌套props 的可变元组

如何获取受类型脚本泛型约束的有效输入参数

NPM使用Vite Reaction应用程序运行预览有效,但我无法将其部署到Netlify

在Reaction中折叠手风琴

打印脚本中正则函数和函数表达式的不同类型缩小行为

我如何键入它,以便具有字符串或数字构造函数的数组可以作为字符串或数字键入s或n

记录的子类型<;字符串,X>;没有索引签名

通过辅助函数获取嵌套属性时保留类型

无法使用prisma中的事务更新记录

Typescript 和 Rust 中跨平台的位移数字不一致

带有 Select 器和映射器的Typescript 泛型

如何在Typescript 中定义具有相同类型的两个泛型类型的两个字段?

如何强制对象数组中的对象属性具有非空字符串值?