Har*_*jan 42 typescript angular
我找到了这个解决方案 有效吗?
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());
});
}
}
Run Code Online (Sandbox Code Playgroud)
这种解决方案适用于离子2.我还可以用于角2吗?
请建议我正确的方法.
Bla*_*ard 82
对于那些想要获得设备高度和宽度的人,即使显示器已调整大小(动态和实时):
在那个组件中: import { HostListener } from "@angular/core";
在组件的类体中写:
@HostListener('window:resize', ['$event'])
onResize(event?) {
this.screenHeight = window.innerHeight;
this.screenWidth = window.innerWidth;
}
Run Code Online (Sandbox Code Playgroud)
在组件中constructor调用初始化变量的onResize方法.另外,不要忘记先声明它们.
constructor() {
this.onResize();
}
Run Code Online (Sandbox Code Playgroud)
完整代码:
import { Component, OnInit } from "@angular/core";
import { HostListener } from "@angular/core";
@Component({
selector: "app-login",
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class FooComponent implements OnInit {
screenHeight: number;
screenWidth: number;
constructor() {
this.getScreenSize();
}
@HostListener('window:resize', ['$event'])
getScreenSize(event?) {
this.screenHeight = window.innerHeight;
this.screenWidth = window.innerWidth;
console.log(this.screenHeight, this.screenWidth);
}
}
Run Code Online (Sandbox Code Playgroud)
Har*_*jan 45
我找到了解决方案.答案很简单.在构造函数中编写以下代码.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
======其他解决方案======
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)
}
}
Run Code Online (Sandbox Code Playgroud)
Bha*_*tel 27
export class Dashboard {
innerHeight: any;
innerWidth: any;
constructor(private router: Router, private http: Http) {
this.innerHeight = (window.screen.height) + "px";
this.innerWidth = (window.screen.width) + "px";
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在这种情况下使用 typescript getter 方法。像这样
public get height() {
return window.innerHeight;
}
public get width() {
return window.innerWidth;
}
Run Code Online (Sandbox Code Playgroud)
并在模板中使用它,如下所示:
<section [ngClass]="{ 'desktop-view': width >= 768, 'mobile-view': width < 768
}"></section>
Run Code Online (Sandbox Code Playgroud)
打印值
console.log(this.height, this.width);
Run Code Online (Sandbox Code Playgroud)
您不需要任何事件处理程序来检查窗口的大小调整,此方法每次都会自动检查大小。
| 归档时间: |
|
| 查看次数: |
80939 次 |
| 最近记录: |