我在angular2+中使用angular2+模板,并试图找到将库导入到试图访问window的组件中的解决方案。Javascript服务使用服务器端预呈现,因此不能将库导入到组件中,这些组件在没有获得窗口的情况下立即尝试访问窗口是没有定义错误的。
目前,我正在尝试将dat.gui添加到我的组件中,但我还没有找到解决方案。https://www.npmjs.com/package/dat.gui
有人知道如何在客户端上加载库,并将其传递给组件以避免在将窗口导入组件时未定义错误?
这将始终抛出错误时,添加到我的组件,因为服务器端预呈现。
const dat = require('dat.gui');
const gui = new dat.GUI();或从‘dat.gui’导入*作为dat;
发布于 2018-02-28 04:36:59
这就是我用来确保浏览器使用窗口、文档、导航器、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();
}
}https://stackoverflow.com/questions/49001489
复制相似问题