You can also create a token for the window, just as Angular does for the Document:
import { InjectionToken } from '@angular/core';
export const WINDOW = new InjectionToken<Window>('WindowToken', {
factory: () => {
if(typeof window !== 'undefined') {
return window
}
return new Window(); // does this work?
}
});
Then you can inject it in the places you need the window object like so:
@Component({
selector: 'my-component',
standalone: true,
imports: [CommonModule],
templateUrl: './my-component.component.html',
styleUrl: './my-component.component.scss',
})
export class RegistrationFormComponent {
private _window = inject(WINDOW); // or window = inject(WINDOW);
inSomeFunction() {
this._window.location.assign('my.url')
}
}
This way, you can keep the benefits of SSR and prerendering and use the window object.