2017-07-27 35 views
4

我一直在嘗試構建響應式導航欄,並且不希望使用媒體查詢,所以我打算使用屏幕分辨率爲*ngIF作爲標準。 但一直面臨問題,因爲我無法找到有關Angular 4屏幕分辨率檢測的任何方法或文檔。我也嘗試了JavaScript方法,但不支持。使用角度檢測屏幕分辨率4

我也曾嘗試following

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

這是在離子

使用和screen.availHeight

但仍然沒有成功。

+0

什麼是「平臺」?這是關於離子嗎? –

+0

@GünterZöchbauer該平臺是有棱角的,只是想提一下我試過的代碼。 – Ronit

+0

['平臺'](https://ionicframework.com/docs/api/platform/Platform)是一種離子服務。所以我猜這是一個離子項目? – robbannn

回答

21

要得到它的init

public innerWidth: any; 
ngOnInit() { 
    this.innerWidth = window.innerWidth; 
} 

如果你想保持更新調整大小:

@HostListener('window:resize', ['$event']) 
onResize(event) { 
    this.innerWidth = window.innerWidth; 
} 
+0

它完美地工作!非常感謝@Eduardo Vargas。 – Ronit

2

的文檔Platformwidth()height(),據說這些方法分別使用window.innerWidthwindow.innerHeight。但是使用這些方法是有優勢的,因爲這些維度是緩存值,這減少了多次且昂貴的DOM讀取的機會。

import { Platform } from 'ionic-angular'; 

... 
private width:number; 
private height:number; 

constructor(private platform: Platform){ 
    platform.ready().then(() => { 
     this.width = platform.width(); 
     this.height = platform.height(); 
    }); 
} 
+0

oug其他答案的作品,我認爲這是一個更好的解決方案 – brijmcq