尝试理解ngrx/data实体数据服务示例here https://ngrx.io/guide/data#creating-entity-data-services,其中显示“创建实体数据服务”。在展示该服务之后,文档继续展示如何在组件中使用 ngrx/data。我感兴趣的组件部分是这样的:
getHeroes() {
this.heroService.getAll();
}
文档指出getAll()
发起一个 HTTP 请求,但我不确定这个请求实际上是在哪里或如何发出的。在里面ngrx-数据仓库 https://github.com/johnpapa/ngrx-data-lab。它指出用以下代码替换 HeroService:
import { Injectable } from '@angular/core';
import {
EntityCollectionServiceBase,
EntityCollectionServiceElementsFactory
} from 'ngrx-data';
import { Hero } from '../core';
@Injectable({ providedIn: 'root' })
export class HeroService extends EntityCollectionServiceBase<Hero> {
constructor(serviceElementsFactory: EntityCollectionServiceElementsFactory) {
super('Hero', serviceElementsFactory);
}
}
文档指出ngrx-data handles getting and saving our data for us
。太好了,但我不知道获取数据是在哪里发生的。我克隆了存储库,检查了finish
分支,无法找到到达端点的东西。
例如。用于调用的服务http.get(${api}/heroes)
for getAll()
获取所有英雄,但这被上面的代码替换了,那么这些调用发生在哪里呢?
我注意到EntityCollectionServiceBase
has a getAll()
方法。但是该服务的配置在哪里进行以注册相应的端点呢?我确信我在这里遗漏了一些非常简单的东西。
从广义上讲EntityCollectionServiceBase
派遣actions并通过坚持effect in EntityEffects
the DefaultDataService
被调用并将响应映射到其成功/失败操作。看架构概述 https://ngrx.io/guide/data/architecture-overview
因此,如果您想访问/转换从 API 返回的数据,您可以扩展/替换DefaultDataService
。您可以使用以下方式注册您的自定义数据服务EntityDataService
它基本上是获取或创建数据服务的数据服务注册表(如果不存在则使用默认值)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)