简体   繁体   English

angular4文档和窗口未定义

[英]angular4 document and window are undefined

I am using dot net core with angular 4 and webpack config. 我使用dot net core with angular 4和webpack config。

When I am trying to get the window or document in a component or service, I am getting this error: 当我试图在组件或服务中获取窗口或文档时,我收到此错误:

ReferenceError: document is not defined ReferenceError:未定义文档

ReferenceError: window is not defined ReferenceError:未定义窗口

Here are the errors i am getting: 以下是我得到的错误:

 Microsoft.AspNetCore.NodeServices[0] ERROR { ReferenceError: window is not defined at _window (C:\\gitRepose\\AngularCore\\FM\\FMWebApp\\FootballWebApp\\ClientApp\\dist\\main-server.js:30801:12) 

 Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0] An unhandled exception has occurred: window is not defined ReferenceError: window is not defined at _window (C:\\gitRepose\\AngularCore\\FM\\FMWebApp\\FootballWebApp\\ClientApp\\dist\\main-server.js:30801:12) 

I am still new to the webpack stuff, anyone know how i can fix this? 我仍然是webpack的新手,有谁知道我可以解决这个问题? I need the window to handle when a window is resizing. 当窗口调整大小时,我需要窗口来处理。

EDIT... 编辑...

I managed to get this fixed by removing the pre render from the index.cshtml 我设法通过从index.cshtml中删除预渲染来解决这个问题

i changed it from this: 我改变了这个:

<app asp-prerender-module="ClientApp/dist/main-server">Loading...</app>

to this: 对此:

<app>Loading...</app>

but now obviously pre rendering on server side won't be happening :/ thus it will slow the app boot up time, any ideas how I can fix this without loosing server side pre rendering? 但现在很明显在服务器端进行预渲染不会发生:/因此它会减慢应用程序启动时间,任何想法如何解决这个问题而不会丢失服务器端预渲染?

If you're using server-side rendering, you should remove all code using window or document that run in node because node doesn't have these variables (node only have global ). 如果您正在使用服务器端呈现,则应使用在节点中运行的windowdocument删除所有代码,因为节点没有这些变量(节点只有global )。

If you still want to use window or document in node env, you can try to use jsdom ( https://github.com/tmpvar/jsdom ). 如果您仍想在节点env中使用windowdocument ,则可以尝试使用jsdom( https://github.com/tmpvar/jsdom )。

If you want to use AOT, use dependency injection to mount window into your components. 如果要使用AOT,请使用依赖注入将窗口装入组件。 In short, write a service that can provide the window object: 简而言之,编写一个可以提供window对象的服务:

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

function getWindow (): any {
    return window;
}

@Injectable()
export class WindowRefService {
    get nativeWindow (): any {
        return getWindow();
    }
}

More information here . 更多信息在这里

Well i'm using angular 4 application and inject the window object using: 好吧,我正在使用角度4应用程序并使用以下方法注入窗口对象:

In Component constructor( @Inject('Window') private window: Window) { } 在Component constructor( @Inject('Window') private window: Window) { }

and In module: providers: [{ provide: 'Window', useValue: window }] 和模块: providers: [{ provide: 'Window', useValue: window }]

and then in the component function i used it like: this.window 然后在组件函数中我使用它: this.window

If you want a quick fix : put this code at the top of your typescript file : 如果您想快速修复:将此代码放在您的打字稿文件的顶部:

 declare var window;
 declare var document;

But if you wanna do it properly : 但如果你想做得恰到好处:

 import { DOCUMENT } from '@angular/platform-browser';
 export const WINDOW           = new InjectionToken( 'WINDOW' );

 @Component()
 export class YourComponent{
          constructor(
              @Inject( WINDOW ) private window,
              @Inject( DOCUMENT ) private document){}

inside your module : 在你的模块内:

providers       : [
    {
        provide  : WINDOW,
        useValue : window
    }
],

This way you can override window and document in your tests and anywhere else. 这样,您可以在测试和其他任何地方覆盖窗口和文档。

In Server Side JavaScript is running in Node Environment which does not have window or document object. 在服务器端,JavaScript在节点环境中运行,该环境没有窗口或文档对象。 Window & Document object works when your javascript code executes in Browser Environment. 当您的JavaScript代码在浏览器环境中执行时,Window&Document对象可以正常工作。 So make sure window or document object related code does not execute on Server Side 因此,请确保窗口或文档对象相关的代码不在服务器端执行

You may use node-jsdom ( https://www.npmjs.com/package/node-jsdom ) if you really need to use window or document object. 如果确实需要使用窗口或文档对象,可以使用node-jsdomhttps://www.npmjs.com/package/node-jsdom )。 Just install 只需安装

$ npm install node-jsdom 

Here ( https://github.com/darrylwest/node-jsdom ) you will get help on node-jsdom 在这里https://github.com/darrylwest/node-jsdom )你会得到node-jsdom的帮助

wrap your code which depends on window like below 包装你的代码依赖于窗口,如下所示

if (typeof window !== 'undefined') {
    // your client-only logic here like below
    alert(window.innerWidth);
}

Place your window. 放置你的窗户。 related code inside componentDidMount function. componentDidMount函数中的相关代码。

In Index.html or Index.cshtml Index.htmlIndex.cshtml中

Change From 改变

<app asp-prerender-module="ClientApp/dist/main-server">Loading...</app>

To

<app asp-ng2-prerender-module="ClientApp/dist/main-server">Loading...</app>

If you are using angular4 in aspnetcore, It should work fine 如果你在aspnetcore中使用angular4,它应该工作正常

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM