angular - 错误:文档未定义,构建角度通用应用程序
问题描述
错误 :
factory(require("jquery"), document, window, navigator);
^ReferenceError: document is not defined
面对角度通用渲染服务器端的问题,我用谷歌搜索了这个并浏览了很多帖子,但没有得到任何有用的资源。
解决方案
Jquery 在浏览器端工作,服务器端不支持浏览器功能。例如,如果您想在 Angular Universal 中使用 jquery,则必须确保仅在浏览器端使用它。
例如,您可以执行以下操作。
在您的 component.ts 文件中导入以下内容。
import { isPlatformServer } from '@angular/common';
import * as $ from 'jquery';
然后在您的 ngOnInit 函数中执行以下操作
constructor(@Inject(PLATFORM_ID) private platformId: any) {}
ngOnInit() {
if (isPlatformBrowser(this.platformId)) {
/* jQuery here */
$("#test-button").click(function () {
alert('WOOOW');
$(this).css("background","#000");
});
}
}
推荐阅读
- java - 无效的 Apache POI HeaderFooterType.EVEN
- python - conda 更新失败
- visual-studio - 如何使用 VS2017 将 PlatformToolset 添加到 VC++ 项目以解决错误 LNK2038?
- java - 硒,找不到元素
- c# - 不同组数的组合
- ldap - 如何将 marklogic 与单点登录集成?
- php - 在laravel的其他项目目录中上传文件
- python - Pyspark - 无法在 hadoop 二进制路径中找到 winutils 二进制文件
- c++ - 与 Castalia Simulator 集成的 Fuzzylite 控制台问题
- amazon-web-services - xrdp ubuntu aws 缺少文本