[英]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.