1

我像这样扩展了 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"])
  );
}
4

1 回答 1

1

您的问题的根本原因可能在此代码中:

getById(): Observable<DidDocument> {
  return this.http.get("/identifiers").pipe(
    map(res => res["didDocument"])
  );
}

在这种情况下,即使DefaultDataServiceConfig正确设置了正确的url,此代码也会覆盖GETurl。

http.get("/identifiers")

相当于

http.get("http://localhost/identifiers")

(如果您的开发应用正在运行localhost

您应该设置绝对或使用withurl的祖先方法: DefaultDataServicesuper

getById(): Observable<DidDocument> {
  return super.getById(id).pipe(
    map(res => res["didDocument"])
  );
}

您可以使用以下代码片段示例在官方文档中找到更多详细信息:

@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)));
  }
}
于 2020-03-05T07:58:13.737 回答