Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度:ngrx观测值_Angular_Rxjs_Observable - Fatal编程技术网

Angular 角度:ngrx观测值

Angular 角度:ngrx观测值,angular,rxjs,observable,Angular,Rxjs,Observable,我有一个包含多个状态的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.

我有一个包含多个状态的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';

存储区中已经有数据。你知道我做错了什么吗?

你肯定所有的状态都发出并完成了吗?尝试使用
CombineTest
?我认为你不需要把观测值作为
forkJoin
的参数放入数组中

当使用
CombineTest
时,每次
子源
可观察到的发射在所有
子源
的初始发射之后,整个东西都发射。如果您不希望
过度发射
场景,那么您可能还需要查看
distinctUntilChanged


1发射,2发射
=>
combineLatest发射
=>
1发射
=>
combineLatest发射
等等。

正如@martin在评论中所说


forkJoin
要求完成所有源观测

因此,您只需获取通过
take(1)
first()
发出的第一个值:

let importRent$=CombineTest(
rentState$.pipe(取(1)),
车辆状态$.pipe(取(1)),
clientState$.pipe(取(1)),
paymentState$.pipe(取(1))
)

您确定所有状态都发出并完成了吗?尝试使用
CombineTest
?我认为你不需要把
可观测值
作为
forkJoin
forkJoin
的参数放在一个数组中,因为它需要完成所有源可观测值。也许在您的情况下,将
这个.store.select('rentState')
管道(take(1))
@ChauTran链接就足够了,您是对的,状态似乎没有完成并发出。我使用了
combinelatetest
,效果很好。你为什么不把它作为答案提交呢?我把它标为答案。非常感谢。