简体   繁体   English

未从服务器端预渲染定义JavascriptServices窗口

[英]JavascriptServices window not defined from server side prerendering

I am using javascriptservices template with angular2+, and I am trying to find a solution for importing libraries into my component that try to access window . 我正在将angular2 +与javascriptservices模板一起使用,并且试图找到一种将库导入到尝试访问window的 组件中的解决方案。 Javascript services use server-side pre-rendering, so you cannot import libraries into components that immediately try to access window without getting window is not defined error. Javascript服务使用服务器端预渲染,因此您无法将库导入立即尝试访问窗口而未获取窗口未定义错误的组件中。

Currently, I am trying to add dat.gui to my component, but I have not found a solution. 当前,我正在尝试将dat.gui添加到我的组件中,但是我没有找到解决方案。 https://www.npmjs.com/package/dat.gui https://www.npmjs.com/package/dat.gui

Does anybody know of a way that I can load the dat.gui library on the client-side , and pass it to my component to avoid the window is not defined error when importing it into a component? 有人知道我可以在客户端加载dat.gui库并将其传递给我的组件以避免将其导入组件时窗口未定义错误的方法吗?

This will always throw an error when added to my component because of the server side pre-rendering. 由于服务器端预渲染,将其添加到我的组件时始终会引发错误。

const dat = require('dat.gui');
const gui = new dat.GUI();

Or Import * as dat from 'dat.gui'; 或从'dat.gui'导入*作为dat;

This is how I use to make sure it's the browser to use window, document, navigator, jQuery operations. 这就是我用来确保它是使用窗口,文档,导航器,jQuery操作的浏览器的方式。

  import {PLATFORM_ID} from '@angular/core'
  import {isPlatformBrowser } from '@angular/common'
    ....


    constructor(@Inject(PLATFORM_ID) private platformId: Object){}

    private isBrowser: boolean = isPlatformBrowser(this.platformId);

    ngAfterViewInit(){
      if (this.isBrowser) {
       const dat = require('dat.gui');
       const gui = new dat.GUI();
      }
    }

暂无
暂无

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

相关问题 由于以下原因,预渲染失败:ReferenceError:未定义窗口 - Prerendering failed because of error: ReferenceError: window is not defined 由于以下原因,预渲染失败:ReferenceError:窗口未定义? - Prerendering failed because of error: ReferenceError: window is not defined? 角度6 | 服务器端渲染:发出“未定义窗口” - Angular 6 | Server side rendering: issue 'window not defined' NodeInvocationException:由于错误而导致预呈现失败:ReferenceError:未定义窗口 - NodeInvocationException: Prerendering failed because of error: ReferenceError: window is not defined 角路由解析器.NET Core 2服务器端预渲染 - Angular route resolver .NET Core 2 server side prerendering ASP.NET CORE 2 + ANGULAR 4:NodeInvocationException:由于错误导致预渲染失败:ReferenceError:未定义窗口 - ASP.NET CORE 2 + ANGULAR 4: NodeInvocationException: Prerendering failed because of error: ReferenceError: window is not defined 异常:调用节点模块失败,并显示以下错误:由于以下原因,渲染失败:ReferenceError:未定义窗口 - Exception: Call to Node module failed with error: Prerendering failed because of error: ReferenceError: window is not defined '$ not defined'| Angular 6中的服务器端渲染 - '$ not defined' | Server side rendering in Angular 6 Webpack从3迁移到4会产生预渲染错误 - Webpack migration from 3 to 4 generate a prerendering error 由于错误,预渲染失败:ReferenceError: Event is not defined with PrimeNG AutoComplete - Prerendering failed because of error: ReferenceError: Event is not defined with PrimeNG AutoComplete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM