NGRX 数据 entityResourceUrl 取决于其他实体。动态网址?
NGRX data entityResourceUrl depending on other entity. dynamic urls?
通常当我想指定实体的 url 时,我有一个特定的 url 作为它的 serverURL/heroes。
但是现在我有一个案例,我想使用属于其他实体的实体。例如,假设我想使用英雄的武器。我的剑的 http urls 看起来像:serverURL/heroes/heroID/weapons。所以假设我需要添加新剑,我唯一的选择是将它添加到特定的英雄,所以我需要知道 heroesID 和剑信息来做类似的事情:HTTP POST http.post('serverURL/heroes/heroID/weapons', swordObject);
目前我只是在 DefaultDataServiceConfig 的 entityHttpResourceUrls 中添加 heroes url。你可以看到example on stackblitz。转到应用程序->商店->实体->实体-store.module.ts。那是我通常指定 url 的地方。
我该如何处理我的案子?如何为子实体设置动态 url?
更新
根据我在这个问题中的发现,ngrx-data 还不支持参数。
您需要使用来自 NGRX 的 router store。
对于商店,您有以下选择器:
selectQueryParams
、selectRouteParams
、selectRouteData
和 selectUrl
.
因此您可以轻松获得构建不同路径的所有需求。
在我的例子中,我创建了服务:
@Injectable()
export class OrganizationDataService extends DefaultDataService<Organization> {
constructor(http: HttpClient, httpUrlGenerator: DefaultHttpUrlGenerator, logger: Logger) {
super('Organization', http, httpUrlGenerator);
}
getAll(): Observable<Organization[]> {
console.log('getAll called');
// custom call to api correct variant:
return this.execute('GET', `${environment.apiUrl}org/${someId}/apps`)
// custom call to api also worked variant:
// return this.http.get<Organization[]>(`${environment.apiUrl}org/${someId}/apps`);
}
}
我最终使用了 getWithQuery 函数:
getWithQuery(params: QueryParams): Observable<Weapon[]> {
const hero= params['heroId'] || '';
const pageIndex = params['pageIndex'] || '0';
const pageSize = params['pageSize'] || '25';
const apiUrl = `api/hero/${heroId}/weapons/?page=${pageIndex}&size=${pageSize}`
return this.http.get(apiUrl)
}
而不是使用 getAll。
通常当我想指定实体的 url 时,我有一个特定的 url 作为它的 serverURL/heroes。
但是现在我有一个案例,我想使用属于其他实体的实体。例如,假设我想使用英雄的武器。我的剑的 http urls 看起来像:serverURL/heroes/heroID/weapons。所以假设我需要添加新剑,我唯一的选择是将它添加到特定的英雄,所以我需要知道 heroesID 和剑信息来做类似的事情:HTTP POST http.post('serverURL/heroes/heroID/weapons', swordObject);
目前我只是在 DefaultDataServiceConfig 的 entityHttpResourceUrls 中添加 heroes url。你可以看到example on stackblitz。转到应用程序->商店->实体->实体-store.module.ts。那是我通常指定 url 的地方。
我该如何处理我的案子?如何为子实体设置动态 url?
更新
根据我在这个问题中的发现,ngrx-data 还不支持参数。
您需要使用来自 NGRX 的 router store。
对于商店,您有以下选择器:
selectQueryParams
、selectRouteParams
、selectRouteData
和 selectUrl
.
因此您可以轻松获得构建不同路径的所有需求。
在我的例子中,我创建了服务:
@Injectable()
export class OrganizationDataService extends DefaultDataService<Organization> {
constructor(http: HttpClient, httpUrlGenerator: DefaultHttpUrlGenerator, logger: Logger) {
super('Organization', http, httpUrlGenerator);
}
getAll(): Observable<Organization[]> {
console.log('getAll called');
// custom call to api correct variant:
return this.execute('GET', `${environment.apiUrl}org/${someId}/apps`)
// custom call to api also worked variant:
// return this.http.get<Organization[]>(`${environment.apiUrl}org/${someId}/apps`);
}
}
我最终使用了 getWithQuery 函数:
getWithQuery(params: QueryParams): Observable<Weapon[]> {
const hero= params['heroId'] || '';
const pageIndex = params['pageIndex'] || '0';
const pageSize = params['pageSize'] || '25';
const apiUrl = `api/hero/${heroId}/weapons/?page=${pageIndex}&size=${pageSize}`
return this.http.get(apiUrl)
}
而不是使用 getAll。