问题描述
大家好,我正在尝试在html文件中使用数据对象,并且正在使用异步管道和主题来发出ID并获取服务器响应。
这是我的代码:
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))
);
我在视图中使用异步管道来使用subscribe for result。
*ngIf="logDetails$ | async; let details"
现在我想要这种行为:我从多个位置发出ID为 getDetails $ 的代码。
然后,我需要在服务器调用结果的 null 值之前将其发射到视图中,然后在某些延迟后服务器响应(LogDetails对象)。
- 发送结果的默认值
- 延迟
- 发送服务器响应
我可以使用运算符来实现吗?
解决方法
您可以使用startWith
和delay
。
this.logDetails$ = this.getDetails$.pipe(
map(id => ApiRoutes.fileLogDetailsApiRoute.replace(":id",id)),switchMap(apiRoute => this.http.get<LogDetails>(apiRoute).pipe(
delay(1000),startWith(null)
)),);