angular - 与 BehaviorSubject 共享数据 api - 架构 Observables Store
问题描述
我只想在 api 中调用并重用 observable 中的数据,就像 redux 中的一种存储一样。
API 网址: http ://demo5095413.mockable.io/consolidado
Stackblitz 项目: https ://stackblitz.com/edit/angular-ivy-iy9zhv?file=src/app/core.service.ts
负责与 api 连接的服务
@Injectable({
providedIn: 'root'
})
export class ConsolidadoApi {
constructor(private http: HttpClient) { }
getInvestiments(): Observable<any> {
return this.http.get<PosicaoConsolidada>(`${environment.basePosicaoConsolidada}`)
.pipe(
map(res => res),
shareReplay(),
)
}
}
处理数据的服务,我的门面层(store) 最初是这样,这个observable负责在api中进行调用。最后我发出一个 BehaviorSubject
export class CoreService {
private subject = new BehaviorSubject<any>([]);
investment$: Observable<any> = this.subject.asObservable();
constructor(private api: ConsolidadoApi, private loadingService: LoadingService, public messagesService: MessagesService, public state: StateService) {
this.getInvesmentsPortifolio$()
}
public getInvesmentsPortifolio$(): Observable<Carteiras> {
return this.api.getInvestiments()
.pipe(
map(response => response.carteiras),
catchError(err => {
const message = "Error Investments";
this.messagesService.showErrors(message);
console.log(message,err);
return throwError(err);
}),
tap(investment$ => this.subject.next(investment$))
)
}
这就是我想做的事情,访问已经拥有所有投资组合的可观察对象,并从那里从投资组合“Agora”创建一个可观察对象
getPortifolioAgora(): Observable<any> {
return this.investment$
.pipe(
map(response => response.carteiras.find(
carteira => carteira.tipoInvestimento === "AGORA"
))
);
}
但在我的组件中不起作用
constructor(public coreService: CoreService, private loadingService: LoadingService) { }
ngOnInit(): void {
this.loadData()
}
public loadData(){
this.loadingService.loadingOn()
this.coreService.getPortifolioAgora().subscribe(res => {
console.log(res)
})
this.coreService.getInvesmentsPortifolio$().subscribe(res => {
this.carteiras = res
console.log('All portifolio investments----->',res)
})
}
我知道如果我这样做,查找工作。
// In this observable I access directly in the api and I can access the investment portfolio Agora
// it's working, but I wouldn't want to do it that way because I would have made another call on the server
public portifolioAgora$(): Observable<Carteiras> {
return this.api.getInvestiments()
.pipe(
map(response => response.carteiras.find(
carteira => carteira.tipoInvestimento === "AGORA"
)),
finalize(() => this.loadingService.loadingOff())
//tap(data => console.log(data, JSON.stringify(data)))
)
}
解决方案
由于getInvesmentsPortifolio$()
您只将carteiras
数组添加到BehaviourSubject
So中,因此在getPortifolioAgora
更新地图代码中
getPortifolioAgora(): Observable<any> {
return this.investment$.pipe(
map((response) =>
response.find((carteira) => carteira.tipoInvestimento === 'AGORA')
)
);
}
问题也可能是您之前调用getPortifolioAgora
过,在初始加载时getInvesmentsPortifolio$
结果将不可用Subject
推荐阅读
- android - 如何使用代码执行触摸和按住操作?
- python - 保持 url 中的文本干净
- python - gensim doc2vec - 如何推断标签
- jquery - 如何在 JQuery 函数之外获取“this”var?
- amazon-web-services - 使用 go 按标签列出 AWS 中的负载均衡器
- shopify - 如何使用ajax从加载的产品页面上的产品中获取产品ID,并在shopify中获取相关的产品ID
- javascript - 多次链接到同一个谷歌文档时如何只打开1个新标签?
- react-native - 当我尝试设置状态时,开玩笑测试失败
- python - 以自定义形式创建新用户时违反非空约束
- angular - 如何在角度 5 中禁用 owl-date-time 选择器中的过去日期