我这样扩展了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
,然后针对自定义root
触发请求。现在,每个请求都命中localhost
(似乎只是在使用当前主机)。
AppStoreModule
不是惰性加载的模块,而是加载到AppModule
中。触发请求的组件是StoreComponent
。我的第一个猜测是,事情并没有按照我期望的顺序发生。
何时创建带有CustomDataService
的{{1}},并且覆盖服务仅在延迟加载的模块中有效吗?
我直接在httpClient中设置了绝对URL,还将标识符传递给CustomConfiguration
方法:
getById
答案 0 :(得分:1)
您的问题的根本原因可能在以下代码中:
getById(): Observable<DidDocument> {
return this.http.get("/identifiers").pipe(
map(res => res["didDocument"])
);
}
在这种情况下,即使使用正确的DefaultDataServiceConfig
正确设置了url
,该代码也会覆盖GET
url。
http.get("/identifiers")
等效于
http.get("http://localhost/identifiers")
(如果您的开发应用程序正在localhost
上投放)
您应该设置绝对url
或将DefaultDataService
的祖先方法与super
结合使用:
getById(): Observable<DidDocument> {
return super.getById(id).pipe(
map(res => res["didDocument"])
);
}
使用以下代码段示例,您可以在official doc中找到更多详细信息:
@Injectable()
export class HeroDataService extends DefaultDataService<Hero> {
constructor(http: HttpClient, httpUrlGenerator: HttpUrlGenerator, logger: Logger) {
super('Hero', http, httpUrlGenerator);
logger.log('Created custom Hero EntityDataService');
}
getById(id: string | number): Observable<Hero> {
return super.getById(id).pipe(map(hero => this.mapHero(hero)));
}
}