获取ngrx选择器的当前值而不订阅。

huangapple go评论75阅读模式
英文:

Get current value of ngrx selector without subscription

问题

state.data
state.dataArchived

我想要从store.data复制数据到state.dataArchived。

selectedSchedulingsOnPopup$ = this.store.pipe(select(selectSchedulingsByBranch));
ngOnInit() {
this.store.dispatch(new GetDirectionsOnGraph({}));
this.selectedSchedulingsOnPopup$.subscribe(value => {
this.selectedSchedulingsOnPopupValue = value;
this.store.dispatch(new GetDirectionsOnGraph(this.selectedSchedulingsOnPopupValue));
});
}

问题是当state.data改变时,state.dataArchived也会改变

所以我想要获取state.data的当前值,而不必订阅它。

英文:

I have my ngrx state this way

state.data
state.dataArchived

I would like to copy data from store.data to state.dataArchived.

selectedSchedulingsOnPopup$ = this.store.pipe(select(selectSchedulingsByBranch));
ngOnInit() {
  this.store.dispatch(new GetDirectionsOnGraph({}));
  this.selectedSchedulingsOnPopup$.subscribe(value => {
    this.selectedSchedulingsOnPopupValue = value;
    this.store.dispatch(new GetDirectionsOnGraph(this.selectedSchedulingsOnPopupValue));
  });
}

the problem is when state.data changes, state.dataArchived changes too

So I would like to get the current value of state.data without subscribing to it.

答案1

得分: 11

I don't understand the question, but if you want to get the value without subscribing to it, you can convert it into a promise.

async ngOnInit() {
  const data = await this.store.pipe(select(selectorForData),
                               take(1)).toPromise();
}

Keep in mind, that in this promise way, it will only react once and get the data and carry on. If you want to be informed of the changes of the data slice, you have to subscribe to it, there is no other way around it.

toPromise is marked as deprecated now. You can use firstValueFrom or lastValueFrom/take(1) to transform observable into promise as below.

async ngOnInit() {
      // firstValueFrom
      const data = await firstValueFrom(this.store.pipe(select(selectorForData));
      // lastValueFrom with take(1)
      const data = await lastValueFrom(this.store.pipe(select(selectorForData),
                                   take(1)));
}
英文:

I don't understand the question, but if you want to get the value without subscribing to it, you can convert it into a promise.

async ngOnInit() {
  const data = await this.store.pipe(select(selectorForData),
                               take(1)).toPromise();
}

Keep in mind, that in this promise way, it will only react once and get the data and carry on. If you want to be informed of the changes of the data slice, you have to subscribe to it, there is no other way around it.

toPromise is marked as deprecated now. You can use firstValueFrom or lastValueFrom/take(1) to transform observable into promise as below.

async ngOnInit() {
      // firstValueFrom
      const data = await firstValueFrom(this.store.pipe(select(selectorForData));
      // lastValueFrom with take(1)
      const data = await lastValueFrom(this.store.pipe(select(selectorForData),
                                   take(1)));
}

答案2

得分: 3

如果您只想获取第一个值,然后忽略所有后续更改,请使用first()操作符:

  ngOnInit() {
    this.store.dispatch(new GetDirectionsOnGraph({}));
    this.selectedSchedulingsOnPopup$.pipe(first())
      .subscribe(value => {
        this.selectedSchedulingsOnPopupValue = value;
        this.store.dispatch(new GetDirectionsOnGraph(this.selectedSchedulingsOnPopupValue));
      });
  }
英文:

If you just want the first value and then to ignore all subsequent changes use the first() operator:

  ngOnInit() {
    this.store.dispatch(new GetDirectionsOnGraph({}));
    this.selectedSchedulingsOnPopup$.pipe(first())
      .subscribe(value => {
        this.selectedSchedulingsOnPopupValue = value;
        this.store.dispatch(new GetDirectionsOnGraph(this.selectedSchedulingsOnPopupValue));
      });
  }

huangapple
  • 本文由 发表于 2020年1月7日 00:55:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定