Detecting real time window size changes in Angular 4

后端 未结 9 755
孤独总比滥情好
孤独总比滥情好 2020-12-04 05:44

I have been trying to build a responsive nav-bar and do not wish to use a media query, so I intend to use *ngIF with the window size as a criterion. But I have

9条回答
  •  囚心锁ツ
    2020-12-04 06:48

    The documentation for Platform width() and height(), it's stated that these methods use window.innerWidth and window.innerHeight respectively. But using the methods are preferred since the dimensions are cached values, which reduces the chance of multiple and expensive DOM reads.

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

提交回复
热议问题