执行Angular Universal App时会出现错误
我正在使用Angular 13应用程序,并试图整合通用。 我遵循官方文件 https://angular.io/guide/universal 进行集成。
当我在终端中击中“ NG运行前端:Serve-SSR”命令时, Angular版本
Angular CLI: 13.3.8
Node: 14.19.3
Package Manager: npm 6.14.17
OS: linux x64
Angular: 13.3.11
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router, service-worker
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1303.8
@angular-devkit/build-angular 13.3.8
@angular-devkit/core 13.3.8
@angular-devkit/schematics 13.3.8
@angular/cdk 13.3.9
@angular/cli 13.3.8
@angular/material 13.3.9
@nguniversal/builders 13.1.1
@nguniversal/express-engine 13.1.1
@schematics/angular 13.3.8
rxjs 7.4.0
typescript 4.6.4
集成了所有内容后,我会收到以下错误。
错误
TypeError [ERR_INVALID_URL]: Invalid URL: null
at new NodeError (internal/errors.js:322:7)
at onParseError (internal/url.js:270:9)
at new URL (internal/url.js:346:5)
at new InitServ (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:51443:17)
at Object.InitServ_Factory [as factory] (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:52031:10)
at R3Injector.hydrate (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:241212:35)
at R3Injector.get (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:241031:33)
at injectInjectorOnly (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:234492:33)
at ɵɵinject (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:234496:61)
at injectArgs (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:234576:23)
I am working angular 13 application and trying to integrate universal.
I have followed the official document https://angular.io/guide/universal to integrate.
Angular version
Angular CLI: 13.3.8
Node: 14.19.3
Package Manager: npm 6.14.17
OS: linux x64
Angular: 13.3.11
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router, service-worker
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1303.8
@angular-devkit/build-angular 13.3.8
@angular-devkit/core 13.3.8
@angular-devkit/schematics 13.3.8
@angular/cdk 13.3.9
@angular/cli 13.3.8
@angular/material 13.3.9
@nguniversal/builders 13.1.1
@nguniversal/express-engine 13.1.1
@schematics/angular 13.3.8
rxjs 7.4.0
typescript 4.6.4
After integrating all when I hit the "ng run Frontend:serve-ssr" command in a terminal, then I get the following error.
Error
TypeError [ERR_INVALID_URL]: Invalid URL: null
at new NodeError (internal/errors.js:322:7)
at onParseError (internal/url.js:270:9)
at new URL (internal/url.js:346:5)
at new InitServ (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:51443:17)
at Object.InitServ_Factory [as factory] (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:52031:10)
at R3Injector.hydrate (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:241212:35)
at R3Injector.get (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:241031:33)
at injectInjectorOnly (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:234492:33)
at ɵɵinject (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:234496:61)
at injectArgs (/home/pc5/Documents/Restucture/frontend-universal/dist/Frontend/server/main.js:234576:23)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查是否已将所有相对路径转换为绝对路径。
要使用Angular Universal并利用Prederning(例如Prederning)的实用程序,您需要遵循一组规则。 查看它!
因为服务器端没有某些对象定义如
document
,窗口
,localstorage
和...当您的代码首先在服务器端渲染时,它将无法识别并导致错误。
我的个人建议如何使用
if(typeof window!=='undefined'& amp; window.document)
逻辑,因此您仅在客户端端使用文档对象。我使用了多米诺骨牌,没有为我工作...
Check if you have converted all relative paths to absolute.
to use angular universal and take advantage of it's utilities like prerendering, you need to follow a set of rules. Check it out !
As server side do not have some objects defined like
document
,window
,localstorage
and ...when your code will be first rendered in server side, it will not be recognized and causes errors.
My personal suggestion how ever is to use
if (typeof window !== 'undefined' && window.document)
logic, hence you will use document object only in client side.I used domino and didn't worked for me ...