我有一個角度4應用程序。在這個應用程序中,我有一個PayPal服務,它執行一些服務器工作並導航(從客戶端)到PayPal。RxJs - 訂閱可觀察到的最後的地方
@Injectable()
export class PaypalService {
private readonly http: HttpApi;
constructor(private readonly httpFactory: HttpFactoryService) {
this.http = httpFactory.get(HttpRequestType.Secured);
}
checkout(transactionId: string):
Observable<Response> {
let subscriber = this.http.post(environment.paymentServer + '/pay', {transactionId: transactionId})
subscriber.subscribe((response:Response) => {
let result = response.json();
window.location.href = result.paymentUrl;
});
return subscriber;
}
}
我希望所有的用戶被告知後,才window.location.href將被調用。
paypal.checkout("1234").subscribe(() =>
//This code should be called before the navigation will start
doSomeCleaning())
我知道我可以使用延遲,但我不知道是否有一個更好的選擇。
嘗試與'flatMap'運營商都流映射例如,在年底只有一個訂閱方法,從中可以調用重定向。 –