问题描述
我正在尝试使用 ngrx/data 来处理我的一个模型上的 CRUD 操作,但由于它是“嵌套的”,我需要在 getById 方法中传递多个参数。有没有办法在数据服务(它扩展默认数据服务)中编写自定义函数,以便可以通过在我的组件中注入的实体服务访问它们?或者,ngrx/data的接口是否支持额外的参数?
@Injectable()
export class AutomationEmailDataService extends DefaultDataService<AutomationEmail> {
constructor(
http: HttpClient,httpUrlGenerator: HttpUrlGenerator,private smService: SettingsManagerService
) {
super('AutomationEmail',http,httpUrlGenerator);
}
// need to pass multiple keys here
getById(automationId,emailId): Observable<AutomationEmail> {
return this.http.get(AutomationsEndpoints.getAutomationEmailInfo(this.smService.getStoreId(),automationId,emailId))
.map((res: AutomationEmail) => new AutomationEmail().deserialize(res));
}
// or,need a custom function that I can access through the entity service
getEmail(automationId,emailId): Observable<AutomationEmail> {
// same http request here
}
}
解决方法
getWithQuery
的 EntityCollectionServiceBase
函数接受 QueryParams
作为参数。
所以在您的数据服务中:
getEmail(automationId,emailId) {
const params = new HttpParams().set("emailId",emailId).set("automationId",automationId);
this.getWithQuery(params);