我当前正在尝试在http请求之后返回Angular中的Observable。以下是方法代码:
public fetchOptions(options ?: any): Observable<any> {
let response: Subject<Response> = new Subject<Response>();
if (!this._config) {
return null;
}
let requestOptions: any= null;
// fetch Token from oag and add the Authorisation Header
this.fetchToken().subscribe((token: IOagAccessTokenResponse) => {
let finalOptions: any= {};
if (options) {
finalOptions = options;
}
if (!finalOptions.headers) {
finalOptions.headers = new HttpHeaders();
}
// first remove Header and after this add the new header
finalOptions.headers = finalOptions.headers.delete('Authorization');
requestOptions = finalOptions;
}), (e): any=> {
};
return TimeoutedObservable.complete(requestOptions);
}
这是上面的TimeoutedObservable.complete()函数的代码,该函数创建并返回一个Observable:
private static timeout: number = 300;
public static complete(obj: any): Observable<any> {
return TimeoutedObservable.completeWithTime(TimeoutedObservable.timeout,
obj);
}
public static completeWithTime(time: number, obj: any): Observable<any> {
let observable: BehaviorSubject<any> = new BehaviorSubject<any>(obj);
setTimeout(()=> {
observable.complete()
}, time);
return observable;
}
这是对此函数的订阅调用: this.fetchOptions(options).subscribe((requestOptions:any)=> { ..... }
此代码在Chrome中有效,但在IE 11中,我收到以下错误:由fetchOptions()函数返回的被订阅的对象requestOptions为空。如何使此代码在IE 11中正常工作?