简体   繁体   English

ReferenceError: window is not defined Angular Universal

[英]ReferenceError: window is not defined Angular Universal

I'm using Angular 10 and trying to implement SSR in my project.我正在使用 Angular 10 并尝试在我的项目中实现 SSR。

When I run the npm run serve:ssr I'm getting the below error当我运行npm run serve:ssr我收到以下错误

ReferenceError: window is not defined

When I googled, they suggested to add domino当我用谷歌搜索时,他们建议添加domino

So below is my server.ts所以下面是我的 server.ts

....
const scripts = fs.readFileSync('dist/asfc-web/browser/index.html').toString();

const window = domino.createWindow(scripts);
global['window'] = window;
global['document'] = window.document;

....

still getting the same error, Please guide me how to resolve this issue.仍然出现相同的错误,请指导我如何解决此问题。

It's simple fix,很简单的解决方法

I've imported the AppServerModule after the global['window'] and it worked我在global['window']之后导入了AppServerModule并且它有效

global['window'] = window;
global['document'] = window.document;

import { AppServerModule } from '../../projects/asfc-web/src/main.server';

you can use Renderer2 listen for this.你可以使用 Renderer2 来监听这个。

import { Renderer2 } from '@angular/core';
 constructor(private renderer2: Renderer2) {
     ...
 }
this.renderer2.listen('window', 'load', event => {    
   this.innerWidth = event.currentTarget.innerWidth;
   console.log(this.innerWidth);
});

You can create new service您可以创建新服务

import {Injectable} from '@angular/core';

function _window(): any {
  return window;
}

@Injectable({
  providedIn: 'root'
})
export class WindowRef {
  get nativeWindow(): any {
    return _window();
  }
}

add in constructor where you want to use:在要使用的构造函数中添加:

  constructor(
    private windowRef: WindowRef
  ) {
  }

and use like this:并像这样使用:

  this.windowRef.nativeWindow.scrollTo({
    top: 0,
    behavior: 'smooth'
  });

or you can check platform:或者您可以检查平台:

  constructor(
    @Inject(PLATFORM_ID) private platformId: any,
    private windowRef: WindowRef
  ) {
  }
if (isPlatformBrowser(this.platformId)) {
  this.windowRef.nativeWindow.scrollTo({
    top: 0,
    behavior: 'smooth'
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReferenceError: window is not defined in angular universal - ReferenceError: window is not defined in angular universal Angular 9 通用参考错误:window 未定义 - Angular 9 Universal ReferenceError: window is not defined Angular 通用错误 ReferenceError: MouseEvent is not defined - Angular Universal error ReferenceError: MouseEvent is not defined 获取ReferenceError:Angular 4 Universal中未定义HTMLInputElement - Getting ReferenceError: HTMLInputElement is not defined with Angular 4 Universal ReferenceError:导航器未在 angular-universal-pwa 中定义 - ReferenceError: navigator is not defined in angular-universal-pwa 如何修复 angular 6 universal server.js 中的“ReferenceError: window is not defined”错误 - How to fix "ReferenceError: window is not defined" error in angular 6 universal server.js ReferenceError:在Angular中使用jsPDF时未定义窗口 - ReferenceError: window is not defined when using jsPDF in Angular Angular5 webpack ReferenceError:窗口未定义 - Angular5 webpack ReferenceError: window is not defined 使用 RxJs WebSocketSubject 和 Angular Universal 时出现“ReferenceError: WebSocket is not defined” - "ReferenceError: WebSocket is not defined" when using RxJs WebSocketSubject and Angular Universal Angular 7 Universal(服务器端渲染)[ERROR ReferenceError:document not defined] - Angular 7 Universal (Server Side Rendering) [ERROR ReferenceError: document is not defined]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM