[英]ReferenceError: window is not defined in angular universal
I am trying to use @nguniversal/express-engine
and I have installed and trying to run it but its giving error in main.js file.我正在尝试使用
@nguniversal/express-engine
并且我已经安装并尝试运行它,但是它在 main.js 文件中给出了错误。
This is the error I am getting这是我得到的错误
C:\Folder\ssr\dist\ssr\server\main.js:179450
})(window, function() {
^
ReferenceError: window is not defined
at Object.rdXg (C:\Folder\ssr\dist\ssr\server\main.js:179450:4)
at __webpack_require__ (C:\Folder\ssr\dist\ssr\server\main.js:26:30)
at Module.+PDj (C:\Folder\ssr\dist\ssr\server\main.js:133:66)
at __webpack_require__ (C:\Folder\ssr\dist\ssr\server\main.js:26:30)
at Module.xCqK (C:\Folder\ssr\dist\ssr\server\main.js:198481:92)
at __webpack_require__ (C:\Folder\ssr\dist\ssr\server\main.js:26:30)
at Module.xLoe (C:\Folder\ssr\dist\ssr\server\main.js:200042:86)
at __webpack_require__ (C:\Folder\ssr\dist\ssr\server\main.js:26:30)
at Module.Mm/0 (C:\Folder\ssr\dist\ssr\server\main.js:89135:105)
at __webpack_require__ (C:\Folder\ssr\dist\ssr\server\main.js:26:30)
A server error has occurred.
node exited with 1 code.
connect ECONNREFUSED 127.0.0.1:59195
I have tried many thing but nothing working.我尝试了很多东西,但没有任何效果。
print function causing the issue and the print function is been used by print-js library print function 导致问题和 print function 被 print-js 库使用
const contentToConvert = document.getElementById('content');
this.selectedFunctionCode = htmlToImage.toPng;
const debugBase64 = this.debugBase64;
this.selectedFunctionCode(contentToConvert)
.then((dataUrl) => {
print(dataUrl, 'image');
})
.catch((error) => {
console.error('oops, something went wrong!', error);
});
because some object like localstorage, window use in client side are not defined in server side you must first check your browser platform is ready then work with these objectes for this you can do like:因为一些 object 像 localstorage,window 在客户端使用没有在服务器端定义你必须首先检查你的浏览器平台准备好然后使用这些对象,你可以这样做:
at first generate check-is-browserService.service.ts like this:首先生成 check-is-browserService.service.ts 像这样:
export class CheckIsBrowserService {
private isBrowser = new BehaviorSubject<boolean>(null);
constructor() {}
getIsBrowser(): Observable<any> {
return this.isBrowser;
}
setIsBrowser(value: any) {
this.isBrowser.next(value);
}
}
app.component.ts app.component.ts
constructor(
@Inject(PLATFORM_ID) private platformId: any,
private checkIsBrowserService: CheckIsBrowserService
){
this.checkIsBrowserService.setIsBrowser(isPlatformBrowser(platformId));
}
then generate service for example window.service.ts然后生成服务,例如 window.service.ts
class Window implements Window {
readonly innerWidth: number;
}
@Injectable({
providedIn: 'root',
})
export class WindowService {
private config: Window;
constructor(private checkIsBrowserService: CheckIsBrowserService) {
this.config = new Window();
this.checkIsBrowserService.getIsBrowser().subscribe((isBrowser) => {
if (isBrowser) {
this.config = window;
}
});
}
innerWidth() {
return this.config.innerWidth;
}
}
and when in your component use window.innerwidth you must change it to并且在您的组件中使用 window.innerwidth 时,您必须将其更改为
x.component.ts: x.component.ts:
constructor(private window: WindowService) {
if (this.window.innerWidth() <= 1024) {
//your code
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.