简体   繁体   English

Angular Rxjs:使用异步管道连续延迟发出所有合并的可观察对象

[英]Angular Rxjs: emit all merged observables with delay continuously with async pipe

Hello guys I'm trying to use a data object in html file and i'm using the async pipe and a subject to emit id and get server response.大家好,我正在尝试在 html 文件中使用数据对象,并且我正在使用异步管道和一个主题来发出 id 并获得服务器响应。
Here is my code:这是我的代码:

     logDetails$: Observable<LogDetails>;
     getDetails$ = new Subject<string>();

     this.logDetails$ = this.getDetails$.pipe(
        map(id => ApiRoutes.fileLogDetailsApiRoute.replace(":id", id)),
        switchMap(apiRoute => this.http.get<LogDetails>(apiRoute))
    );

I use an async pipe in my view to use subscribe for result.我在我的视图中使用异步管道来使用订阅结果。

*ngIf="logDetails$ | async; let details"

Now i want this behaviour: I emit the getDetails$ with id from multiple locations.现在我想要这种行为:我从多个位置发出带有 id 的getDetails$
Then i need that before server call a null value for result get emmited to the view and then the server response (LogDetails object) after some delay.然后我需要在服务器调用结果的值之前将其发送到视图,然后在一些延迟后发送服务器响应(LogDetails 对象)

  1. send a default value for result发送结果的默认值
  2. delay延迟
  3. send server response发送服务器响应

Can i use operators to achieve this?我可以使用运算符来实现这一点吗?

You can use startWith and delay .您可以使用startWithdelay

this.logDetails$ = this.getDetails$.pipe(
  map(id => ApiRoutes.fileLogDetailsApiRoute.replace(":id", id)),
  switchMap(apiRoute => this.http.get<LogDetails>(apiRoute).pipe(
    delay(1000),
    startWith(null)
  )),
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM