您可以尝试引入subject
触发器获取:
private fetchTrigger = new BehaviorSubject(null);
triggerFetch() {
this.fetchTrigger.next();
}
fetch() {
this.userObservable = Observable
.combineLatest(
Observable.fromPromise(this.getToken()),
this.fetchTrigger
)
.switchMap(([token]) => {
...
})
...
.publishLast()
.refCount();
}
请注意,此示例token
仅检索一次.如果您需要在每次刷新时获得新令牌:
private fetchTrigger = new BehaviorSubject(null);
triggerFetch() {
this.fetchTrigger.next();
}
fetch() {
this.userObservable = this.fetchTrigger
.switchMap(() => Observable.fromPromise(this.getToken()))
.switchMap(token => {
...
})
...
.publishLast()
.refCount();
}