【问题标题】:NGRX data entityResourceUrl depending on other entity. dynamic urls?NGRX 数据 entityResourceUrl 取决于其他实体。动态网址?
【发布时间】:2020-10-29 22:08:44
【问题描述】:

通常当我想指定实体的 url 时,我有一个特定的 url,例如 serverURL/heroes。 但是现在我有一个案例,我想使用属于其他实体的实体。例如,假设我想使用英雄的武器。我的剑 http url 看起来像:serverURL/heroes/heroID/weapons。所以假设我需要添加新剑,我唯一的选择是将其添加到特定英雄,所以我需要知道 herosID 和剑信息才能执行以下操作:HTTP POST http.post('serverURL/heroes/heroID/weapons', swordObject);

目前我只是在 DefaultDataServiceConfig 中的 entityHttpResourceUrls 中添加英雄 url。你可以看到example on stackblitz。转到 app->store->entity-> entity-store.module.ts。那是我通常指定网址的地方。

如何处理我的案件?如何获得子实体的动态网址?

更新

根据我在这个问题中的发现,ngrx-data 还不支持参数。

https://github.com/ngrx/platform/issues/1934

【问题讨论】:

    标签: angular ngrx angular-ngrx-data


    【解决方案1】:

    就我而言,我创建了服务:

    @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`);
      }
    }
    

    【讨论】:

    • 这是我现在正在做的,但这只会返回 observable 它不适用于 store。
    • @Vladymyr K someId 来自哪里?这是我现在的问题。
    • @Godrules500 您是否设法解决了这个问题?有同样的问题
    【解决方案2】:

    您需要使用来自 NGRX 的 router store

    有了商店,你就有了这个选择器:

    selectQueryParamsselectRouteParamsselectRouteDataselectUrl

    因此,您可以轻松满足构建不同路径的所有需求。

    【讨论】:

      【解决方案3】:

      我最终使用了 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。

      【讨论】:

        猜你喜欢
        • 2020-02-12
        • 1970-01-01
        • 2020-11-07
        • 2018-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-11
        • 1970-01-01
        相关资源
        最近更新 更多