从不使用 Angular 服务器端渲染的资产中获取文件

Fetching File from Assets not Working with Angular Server Side Rendering

我在运行时使用以下代码从资产文件夹加载 json 文件:

initializeApp = () =>
  new Promise(async resolve => {
    this.settings await this.http.get(`/assets/file.json`).toPromise();
    resolve();
  });

使用 ng serve 编译 angular 应用程序时,这段代码工作正常。不过我也想支持server side rendering。使用 npm run build:ssr 构建服务器并随后使用 npm run serve:ssr 运行 构建服务器时,我在节点服务器上收到以下错误:

ERROR { Error: Uncaught (in promise): Error
    at resolvePromise (/project/services/frontend/dist/server.js:1003:31)
    at /project/services/frontend/dist/server.js:913:17
    at rejected (/project/services/frontend/dist/server/main.js:136313:93)
    at ZoneDelegate.invoke (/project/services/frontend/dist/server.js:542:26)
    at Object.onInvoke (/project/services/frontend/dist/server/main.js:118689:33)
    at ZoneDelegate.invoke (/project/services/frontend/dist/server.js:541:52)
    at Zone.run (/project/services/frontend/dist/server.js:301:43)
    at /project/services/frontend/dist/server.js:1061:34
    at ZoneDelegate.invokeTask (/project/services/frontend/dist/server.js:574:31)
    at Object.onInvokeTask (/project/services/frontend/dist/server/main.js:118670:33)

在服务器端添加解析或拒绝,因为您的承诺未被任何解析器处理或拒绝。

使用angular通用时,URL必须是绝对的。因此,要么在您的配置中的某处设置基础 URL,要么从基础网络服务器中检索它,如 documentation

中所示