Ayo*_*b.A 4 observable rxjs angular
我有一个包含多个状态的 ngrx 商店。我试图通过收集每个州的数据来构造一个对象(Rent 对象):这是我的代码:
ngOnInit() {
//Fetch the rent and its properties data
const rentID = +this.route.snapshot.params['id'];
let rentState$ = this.store.select('rentState');
let clientState$ = this.store.select('clientState');
let vehiculeState$ = this.store.select('vehiculesState');
let paymentState$ = of(null);//TODO
let importRent$ = forkJoin([rentState$, vehiculeState$, clientState$, paymentState$])
.pipe(mergeMap(([rentData, vehiculeData, clientData, paymentData]: [any, any, any, any]) => {
let rent = new Rent();
rent = rentData.rentList.filter(rent => rent.id === rentID)[0];
rent.vehicule = vehiculeData.vehicules.filter(vehicule => vehicule.id === this.rent.vehicule.id)[0];
rent.client = clientData.clientList.filter(client => client.id === this.rent.client.id)[0];
rent.companion = clientData.companionList.filter(companion => companion.id === this.rent.companion.id)[0];
//TODO: retrieve payments
return of(rent);
}))
importRent$.subscribe(data => console.log('hello'));
}
Run Code Online (Sandbox Code Playgroud)
但我的控制台中没有收到任何消息“你好”。由于某种原因,代码订阅不会发生。
我的代码中已经有这些导入:
import { Observable } from 'rxjs/Observable';
import { forkJoin } from 'rxjs/observable/forkJoin';
import { of } from 'rxjs/observable/of';
import { mergeMap } from 'rxjs/operators';
Run Code Online (Sandbox Code Playgroud)
商店里已经有数据了。知道我做错了什么吗?
您确信所有状态都会发出并完成吗?尝试用combineLatest?我认为您不需要将可观察量作为 的参数放入数组中forkJoin。
在使用时combineLatest,每次child-sourceObservable 在 all 的初始发射之后发射child-source,整个事物都会发射。distinctUntilChanged如果您不想要场景,您可能也想研究一下over-emitting。
1 emits, 2 emits=> combineLatest emits=> 1 emits=>combineLatest emits等等...
| 归档时间: |
|
| 查看次数: |
7621 次 |
| 最近记录: |