我添加了@ ng-toolkit / universal
ng add @ng-toolkit/universal
我有很多错误,所以我要建立备份的项目,并一次测试每个组件/功能。现在大多数事情都在起作用,但是,我似乎只停留在一个问题上-
我想在应用加载时预加载一些资产,所以我最初有这行,在ng-toolkit / universal
app.module.ts
providers: [WorkService, { provide: APP_INITIALIZER, useFactory: workProviderFactory, deps: [WorkService], multi: true }],
和中
WorkService.service.ts
import {Injectable} from '@angular/core';
import {HttpClient, HttpResponse} from '@angular/common/http';
import {Project} from '../models/work-model';
import * as THREE from 'three';
@Injectable()
export class WorkService {
private timeout = 10000;
private errorMessages = {
0: 'internet-error',
500: 'server-error',
timeout: 'timeout-error'
};
private data: Project[];
private tileTextures: any = [];
constructor(private http: HttpClient) {
}
getData(): any {
return this.data;
}
load(): Promise<any> {
let promise = new Promise((resolve, reject) => {
this.http.get('/assets/case-studies/work.json')
.toPromise()
.then(
res => {
this.data = res['work'];
resolve(true);
},
msg => {
reject(msg);
}
)
.catch((err) => console.log("rejected:", err));
});
return promise;
}
}
但是我收到以下错误-
ERROR [Error]
[Error]
ERROR { Error: Uncaught (in promise): Error
at resolvePromise (dist/server.js:200857:31)
at resolvePromise (dist/server.js:200814:17)
at dist/server.js:200916:17
at ZoneDelegate.module.exports.ZoneDelegate.invokeTask (dist/server.js:200464:31)
at Object.onInvokeTask (dist/server.js:4192:33)
at ZoneDelegate.module.exports.ZoneDelegate.invokeTask (dist/server.js:200463:36)
at Zone.module.exports.Zone.runTask (dist/server.js:200231:47)
at drainMicroTaskQueue (dist/server.js:200638:35)
at module.exports.ZoneTask.invokeTask (dist/server.js:200543:21)
at Server.ZoneTask.invoke (dist/server.js:200528:48)
rejection: [Error],
promise: ZoneAwarePromise { __zone_symbol__state: 0, __zone_symbol__value: [Error] },
答案 0 :(得分:1)
我认为,如果您使用的是Universal,则需要使用绝对路径。 /assets/case-studies/work.json
在节点服务器上运行时将不起作用。看到这个issue