[英]Angular 7 Universal (Server Side Rendering) [ERROR ReferenceError: document is not defined]
I have implemented SSR for my project and deployed it to heroku. 我已经为我的项目实现了SSR并将其部署到了heroku。 Now, when I start app in
heroku logs --tail
I get a lot of things running and it lowers performance significantly. 现在,当我在
heroku logs --tail
启动应用程序时,我运行了很多东西,这大大降低了性能。 Most of the things are errors: 大多数事情都是错误的:
ERROR ReferenceError: document is not defined
错误ReferenceError:未定义文档
Even though I am using constructions like this: 即使我使用这样的结构:
if (isPlatformBrowser(this.platformId)) {
const overflow = this.isMenuOpened ? 'hidden' : 'auto;';
this.renderer.setStyle(this.document.body, 'overflow', overflow);
}
Each time I reload page, this thing starts running again. 每次重新加载页面时,这个东西都会重新开始运行。
In my package.json
file I have these commands to run: 在我的
package.json
文件中,我运行以下命令:
"scripts": {
"ng": "ng",
"start": "node dist/server.js",
"build": "npm run build:server:prod && npm run build:browser:prod",
"build:server:prod": "ng run oilEnergy:server && webpack --config webpack.server.config.js --progress --colors",
"build:browser:prod": "ng build --prod"
}
So when Heroku is being deployed, it builds server and browser folders and compiles server.js file, which is then responsible for running my app. 因此,当部署Heroku时,它会构建服务器和浏览器文件夹并编译server.js文件,然后该文件负责运行我的应用程序。
After some time my app crashes and heroku shows Application error
instead of my app. 一段时间后,我的应用程序崩溃,heroku显示
Application error
而不是我的应用程序。
I have no idea, why is this happening. 我不知道,为什么会这样。 Maybe somebody had this issue before?
也许以前有人遇到过这个问题?
try with document
instead of this.document
尝试使用
document
而不是this.document
following code is working for me 以下代码对我有用
import { PLATFORM_ID, Inject } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
constructor(
@Inject(PLATFORM_ID) private platformId: Object) {
if (isPlatformBrowser(this.platformId)) {
this.bodyTag = document.getElementsByTagName('body')[0];
this.htmlTag = document.getElementsByTagName('html')[0];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.