Dil*_*Pal 5 angular-universal angular
错误 :
factory(require("jquery"), document, window, navigator);
^ReferenceError: document is not defined
Run Code Online (Sandbox Code Playgroud)
面对角度通用渲染服务器端的问题,我用谷歌搜索了这个并浏览了很多帖子,但没有得到任何有用的资源。
Jquery在浏览器端工作,服务器端不支持浏览器功能。例如,如果您想在 Angular Universal 中使用 jquery,则必须确保仅在浏览器端使用它。
例如,您可以执行以下操作。
在您的 component.ts 文件中导入以下内容。
import { isPlatformServer } from '@angular/common';
import * as $ from 'jquery';
Run Code Online (Sandbox Code Playgroud)
然后在 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");
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13573 次 |
| 最近记录: |