Another approach that I took was
import {Component, OnInit} from '@angular/core';import {fromEvent} from "rxjs";import {debounceTime, map, startWith} from "rxjs/operators";function windowSizeObserver(dTime = 300) { return fromEvent(window, 'resize').pipe( debounceTime(dTime), map(event => { const window = event.target as Window; return {width: window.innerWidth, height: window.innerHeight} }), startWith({width: window.innerWidth, height: window.innerHeight}) );}@Component({ selector: 'app-root', template: `<h2>Window Size</h2><div><span>Height: {{(windowSize$ | async)?.height}}</span><span>Width: {{(windowSize$ | async)?.width}}</span></div> `})export class WindowSizeTestComponent { windowSize$ = windowSizeObserver();}
here the windowSizeObserver
can be reused in any component