AngularNgRx组件存储.switchMap首先发出旧值
创始人
2024-10-28 18:00:41
0

该问题的解决方法是使用exhaustMap替代switchMap。exhaustMap等待前一个内部Observable完成后才会触发下一个内部Observable,并忽略中间的值。这确保了我们始终使用最新的值。

代码示例:

import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { exhaustMap, map } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { AppState } from './app.state';

@Component({
  selector: 'app-root',
  template: `
    

{{ title$ | async }}

`, }) export class AppComponent implements OnInit { title$: Observable; constructor(private store: Store) {} ngOnInit() { this.title$ = this.store.select('title').pipe( exhaustMap((title) => { return this.doSomethingAsync(title); }) ); } doSomethingAsync(value: string): Observable { return of(value).pipe( delay(1000), map((title) => { return `Hello ${title}!`; }) ); } }

相关内容

热门资讯

推荐透视!黑侠破解wepoke... 推荐透视!黑侠破解wepoker,微乐自建房辅助工具,积累教程(有挂功能)-哔哩哔哩黑侠破解wepo...
必备透视!红龙poker辅助平... 必备透视!红龙poker辅助平台,微乐四川小程序修改器,指引教程(有挂秘诀)-哔哩哔哩;1、该软件可...
解迷透视!wepoker透视底... 解迷透视!wepoker透视底牌,微信小程序微乐辅助器免费版v2.0,大纲教程(有挂规律)-哔哩哔哩...
开挂透视!红龙poker辅助平... 开挂透视!红龙poker辅助平台,微信微乐开挂,演示教程(有挂规律)-哔哩哔哩1、任何红龙poker...
详细透视!pokermaste... 详细透视!pokermaster脚本,微乐安徽小程序辅助,方式教程(有挂方法)-哔哩哔哩pokerm...
了解透视!hhpoker德州机... 了解透视!hhpoker德州机器人,微乐自建房免费脚本哪个最好用,练习教程(有挂细节)-哔哩哔哩一、...
解谜透视!fishpoker透... 解谜透视!fishpoker透视底牌,微乐河南小程序辅助器免费,绝活教程(有挂讲解)-哔哩哔哩1、超...
有挂透视!德州透视脚本,微乐广... 有挂透视!德州透视脚本,微乐广西自建房免费黑科技下载,指引教程(发现有挂)-哔哩哔哩暗藏猫腻,小编详...
有挂透视!pokeplus脚本... 有挂透视!pokeplus脚本,收集微乐小程序游戏破解器,资料教程(存在有挂)-哔哩哔哩1、有挂透视...
关于透视!epoker免费透视... 关于透视!epoker免费透视脚本,微信微乐辅助器下载,大纲教程(有挂教程)-哔哩哔哩epoker免...