【发布时间】:2019-12-20 14:36:55
【问题描述】:
在当前项目中,我正在 Nrwl/Nx 工作区上实现 Angular 服务器端渲染。我严格按照 Nrwl 的这些guidelines。在运行节点应用程序之前一切正常。该项目包含一个名为 Plotly.js 的外部库,不幸的是它使用了document。它抛出以下错误
/Users/PATHTOPROJECT/node_modules/plotly.js/dist/plotly.js:105975
var style = document.getElementById(id);
^
ReferenceError: document is not defined
Plotly.js 被导入到延迟加载的 NgModule 中
import * as PlotlyJS from 'plotly.js/dist/plotly.js';
import { PlotlyModule } from 'angular-plotly.js';
PlotlyModule.plotlyjs = PlotlyJS;
当然,在 SSR 中,document 不可用。 Plotly.js 仅用于延迟加载的路由。我试图保护这条路线只能在isPlatformBrowser 上使用,但这并不能解决问题。也许有可能添加一个“假”document 或类似的东西?
你有什么想法吗?谢谢和欢呼!
【问题讨论】:
-
你在哪里包含/使用 plotly.js?
-
你试过
ngAfterViewInit()吗?
标签: angular plotly server-side-rendering angular-universal