Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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/Rxjs运行存储(ngrx)同步调用_Angular_Async Await_Rxjs_Ngrx - Fatal编程技术网

Angular/Rxjs运行存储(ngrx)同步调用

Angular/Rxjs运行存储(ngrx)同步调用,angular,async-await,rxjs,ngrx,Angular,Async Await,Rxjs,Ngrx,在从我的存储(ngrx)检索到2个对象之后,我需要执行一个操作,因此,我需要在执行操作之前响应这两个调用,如下所示: const item1$: Observable<Item> = this._store$.select( ItemStoreSelectors.selectItemById(this.id1) ); const item2$: Observable<Item> = this._store$.select( ItemStoreSelectors.

在从我的存储(ngrx)检索到2个对象之后,我需要执行一个操作,因此,我需要在执行操作之前响应这两个调用,如下所示:

const item1$: Observable<Item> = this._store$.select(
  ItemStoreSelectors.selectItemById(this.id1)
);

const item2$: Observable<Item> = this._store$.select(
  ItemStoreSelectors.selectItemById(this.id2)
);

let item1: Item;
item1$.pipe(take(1)).subscribe((item: Item) => {
  item1 = item;
});

let item2: Item;
item2$.pipe(take(1)).subscribe((item: Item) => {
  item2 = item;
});

// here, items might not have been initialized
doSomething(item1, item2);
const item1$:Observable=this.\u store$。选择(
ItemStoreSelectors.selectItemById(this.id1)
);
const item2$:Observable=this.\u store$。选择(
ItemStoreSelectors.selectItemById(this.id2)
);
让项目1:项目;
item1$.pipe(take(1)).subscribe((item:item)=>{
项目1=项目;
});
让项目2:项目;
item2$.pipe(取(1)).subscribe((项目:项目)=>{
项目2=项目;
});
//在这里,项目可能尚未初始化
剂量测定法(第1项、第2项);
我试图在rxjs中寻找一个带有switchMap、mergeMap等的解决方案,但无法将其应用于我的需要。我想我找到了一个执行async/await的解决方案,但我不确定这是否是一个好的实践


感谢您的帮助。

重构为以下代码:

import { forkJoin } from 'rxjs';

const item1$: Observable<Item> = this._store$.select(
  ItemStoreSelectors.selectItemById(this.id1)
);

const item2$: Observable<Item> = this._store$.select(
  ItemStoreSelectors.selectItemById(this.id2)
);

forkJoin([item1$.pipe(take(1)), item2$.pipe(take(1))])
   .subscribe(([item1,item2])=>doSomething(item1,item2));
  
从'rxjs'导入{forkJoin};
const item1$:Observable=this.\u store$。选择(
ItemStoreSelectors.selectItemById(this.id1)
);
const item2$:Observable=this.\u store$。选择(
ItemStoreSelectors.selectItemById(this.id2)
);
forkJoin([item1$.pipe(take(1)),item2$.pipe(take(1))]))
.订阅(([item1,item2])=>doSomething(item1,item2));

重构为以下代码:

import { forkJoin } from 'rxjs';

const item1$: Observable<Item> = this._store$.select(
  ItemStoreSelectors.selectItemById(this.id1)
);

const item2$: Observable<Item> = this._store$.select(
  ItemStoreSelectors.selectItemById(this.id2)
);

forkJoin([item1$.pipe(take(1)), item2$.pipe(take(1))])
   .subscribe(([item1,item2])=>doSomething(item1,item2));
  
从'rxjs'导入{forkJoin};
const item1$:Observable=this.\u store$。选择(
ItemStoreSelectors.selectItemById(this.id1)
);
const item2$:Observable=this.\u store$。选择(
ItemStoreSelectors.selectItemById(this.id2)
);
forkJoin([item1$.pipe(take(1)),item2$.pipe(take(1))]))
.订阅(([item1,item2])=>doSomething(item1,item2));

CombineTest将在每次一次可观察到的火灾时触发,如果这是您想要的,请尝试以下方法:

item1$: Observable<Item> = this._store$.select(
  ItemStoreSelectors.selectItemById(this.id1)
);

item2$: Observable<Item> = this._store$.select(
  ItemStoreSelectors.selectItemById(this.id2)
);

result$: Observable<any> = combineLatest(this.item1$, this.item2$, this.doSomething());
item1$:Observable=this.\u store$。选择(
ItemStoreSelectors.selectItemById(this.id1)
);
item2$:Observable=此项。\存储$。选择(
ItemStoreSelectors.selectItemById(this.id2)
);
结果$:Observable=CombineTest(this.item1$、this.item2$、this.doSomething());

CombineTest将在每次一次可观察到的火灾时触发,如果这是您想要的,请尝试以下方法:

item1$: Observable<Item> = this._store$.select(
  ItemStoreSelectors.selectItemById(this.id1)
);

item2$: Observable<Item> = this._store$.select(
  ItemStoreSelectors.selectItemById(this.id2)
);

result$: Observable<any> = combineLatest(this.item1$, this.item2$, this.doSomething());
item1$:Observable=this.\u store$。选择(
ItemStoreSelectors.selectItemById(this.id1)
);
item2$:Observable=此项。\存储$。选择(
ItemStoreSelectors.selectItemById(this.id2)
);
结果$:Observable=CombineTest(this.item1$、this.item2$、this.doSomething());

或者,您可以在效果上使用withLatestFrom。像这样的

@Effect()
  myAction$ = this.actions$.pipe(
    ofType<MyAction>(MyActionTypes.MyAction),
    withLatestFrom(this.store.select(mySelector)),
    withLatestFrom(this.store.select(myOtherSelector)),
    exhaustMap(([[action, item1], item2]) =>
      this.myService.myRequest(/*...*/)
      .pipe(
        map(myResult => new GetResultSuccess(myResult)),
        catchError(error => of(new GetResultFailure(error))),
      ),
    ),
  );
@Effect()
myAction$=此.actions$.pipe(
类型(MyActionTypes.MyAction),
withLatestFrom(this.store.select(mySelector)),
withLatestFrom(this.store.select(myOtherSelector)),
排气图(([[行动,项目1],项目2])=>
this.myService.myRequest(/*…*/)
.烟斗(
映射(myResult=>new GetResultAccess(myResult)),
catchError(error=>of(新GetResultFailure(error)),
),
),
);

或者,您可以在效果上使用withLatestFrom。像这样的

@Effect()
  myAction$ = this.actions$.pipe(
    ofType<MyAction>(MyActionTypes.MyAction),
    withLatestFrom(this.store.select(mySelector)),
    withLatestFrom(this.store.select(myOtherSelector)),
    exhaustMap(([[action, item1], item2]) =>
      this.myService.myRequest(/*...*/)
      .pipe(
        map(myResult => new GetResultSuccess(myResult)),
        catchError(error => of(new GetResultFailure(error))),
      ),
    ),
  );
@Effect()
myAction$=此.actions$.pipe(
类型(MyActionTypes.MyAction),
withLatestFrom(this.store.select(mySelector)),
withLatestFrom(this.store.select(myOtherSelector)),
排气图(([[行动,项目1],项目2])=>
this.myService.myRequest(/*…*/)
.烟斗(
映射(myResult=>new GetResultAccess(myResult)),
catchError(error=>of(新GetResultFailure(error)),
),
),
);

根据具体情况使用CombineTest或
forkJoin
@MoxxiManagarm会调查的,谢谢,我已经试过了,我想,但无法让它与我的案例一起工作使用CombineTest或视情况而定
forkJoin
@MoxxiManagarm我会调查一下,谢谢,我已经试过了,但我想不能让它与我的案例一起使用谢谢,它看起来很棒,所以如果我理解得很好,订阅只会在两个项目都被激发时激发?正确,forkJoin创建了一个observable,它会在所有源完成后发出源的最后一个值。谢谢,它看起来很棒,所以如果我理解得很好,subscribe只会在两个项都被激发时激发?正确,forkJoin创建了一个observable,它会在所有源完成后发出源的最后一个值。谢谢,我将研究CombineTest和forkJoin作为@Jota的区别。托莱多回答了,但我认为它不能完全满足我的需要,因为我还需要一个管道(take(1))谢谢,我将研究combinelateest和forkJoin as@Jota之间的区别。托莱多回答了,但我认为它不能完全满足我的需要,因为我需要一根管子(拿(1))