我像这样扩展了 DefaultDataService:
@Injectable()
export class DidDocumentDataService extends DefaultDataService<DidDocument> {
constructor(http: HttpClient, httpUrlGenerator: HttpUrlGenerator) {
super("DidDocument", http, httpUrlGenerator, defaultDataServiceConfig);
}
getById(): Observable<DidDocument> {
return this.http.get("/identifiers").pipe(
map(res => res["didDocument"])
);
}
}
app-store.module.ts
设置如下:
// this used to work before overriting the DefaultDataService
export const defaultDataServiceConfig: DefaultDataServiceConfig = {
root: `${HOST}/1.0/`
};
@NgModule({
imports: [
EntityDataModule.forRoot(entityConfig),
... // other store related imports
],
providers: [
DidDocumentDataService,
{ provide: DefaultDataServiceConfig, useValue: defaultDataServiceConfig }
],
declarations: [StoreComponent],
exports: [StoreComponent]
})
export class AppStoreModule {
constructor(
private entityDataService: EntityDataService,
private didDocService: DidDocumentDataService
) {
this.entityDataService.registerService("DidDocument", this.didDocService);
}
}
在我扩展 DefaultDataService 之前,defaultDataServiceConfig
使用了 并且针对 custom 触发了请求root
。现在每个请求命中localhost
(似乎只使用当前主机)。
AppStoreModule
不是延迟加载的模块,而是加载在AppModule
. 触发请求的组件是StoreComponent
. 我的第一个猜测是某些事情没有按照我期望的顺序发生。
什么时候创建并且CustomDataService
覆盖CustomConfiguration
服务仅在延迟加载的模块中起作用?
解决方案
我直接在 httpClient 中设置了绝对 URL,并将标识符传递给getById
方法:
getById(id: string): Observable<DidDocument> {
return this.http.get(`${HOST}/1.0/identifiers/${id}`).pipe(
map(res => res["didDocument"])
);
}