我有一个消息服务,每当调用API方法时都会发出消息。这个想法是这样的,我的应用程序中的所有其他组件都可以调用服务中的方法来显示错误或成功消息。在将值添加到可观察值前等待x秒
import { Injectable } from '@angular/core';
import { MessagingComponent } from ',/messaging.component';
import { ReplaySubject } from 'rxjs';
@Injectable()
export class MessageService {
public messages$: ReplaySubject<Object> = new ReplaySubject<Object>(1);
constructor() {
this.messages$.next({
message: '',
type: null
});
}
showError(message: string, time: number): void {
this.messages$.next({
message,
type: 'message-error',
time: time
});
hideMessage(){
this.messages$.next({
message: '',
type: null
});
}
的想法是,任何其他组件可以调用messageService.showError('my error message', 3000)
。我很困惑如何让我的showError
方法在3秒钟后调用hideMessage
方法,或者调用者提供给time
的时间长。
听起来像你需要一个['delay'](http://reactivex.io/documentation/operators/delay.html)。 – jonrsharpe