我有一个包含多个状态的 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'));
}
但是我的控制台中没有收到任何消息“你好”。由于某种原因,代码订阅没有发生。
我的代码中已经有这些导入:
import { Observable } from 'rxjs/Observable';
import { forkJoin } from 'rxjs/observable/forkJoin';
import { of } from 'rxjs/observable/of';
import { mergeMap } from 'rxjs/operators';
商店中已有数据。知道我做错了什么吗?
最佳答案
您确定所有状态都发出并完成了吗?试试 combineLatest
?而且我认为您不需要将可观察值作为 forkJoin
的参数放入数组中。
在使用 combineLatest
时,每次 child-source
Observable 在所有 child-source
初始发射之后发射,整个发射。如果您不想要 over-emitting
场景,您可能还想查看 distinctUntilChanged
。
1 emits, 2 emits
=> combineLatest emits
=> 1 emits
=> combineLatest emits
等等。 ..
关于 Angular :ForkJoin ngrx observables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49884085/