通过其他服务调用HTTP API



我正在进行一个服务调用,该调用通过我的组件内部调用http函数,该组件运行良好,我确实得到了响应。然而,当我试图通过相同的服务和另一种方法进行http调用时,它不会被处理,并保持在挂起状态。我已经尝试过在上面创建一个类似的代码。有人能就这个问题提出建议吗?根据我的理解,当通过服务中的方法进行呼叫时,它不会得到订户。然而,我无法在下面的代码中得到这个问题。

需要补充的是,我看到的行为是http get调用不会返回可观察到的内容,并且当通过相同的服务进行调用时,.map函数也不会被调用

@Injectable()
export class MyService {
// GET staff by location
getData(): Observable<any> {
// Set content headers
let headers =     this.createRequestHeader(localStorage.getString("accessToken"));
// Create a request option
let options = new RequestOptions({ headers: headers });
return this.http.get(url, options)
.map(res => {
return res.json()
})
.catch(this.handleErrors);

}

processQueue(): any { 
while (!this.isQueueEmpty()){
this.getData().subscribe((res) => {
this.popQueue();
console.log(res);
});
}}
}
@Component({
selector: "my-component",
moduleId: module.id,
templateUrl: "./my-component.component.html",
styleUrls: ['./my-component.component.css']
})
export class MyComponent implements OnInit, OnDestroy {
ngOnInit(private mySer:MyService): void {
this.mySer.getData().subscribe(
(res) => {
console.log(res);
}
}

如果正确注入了connectivy,请尝试直接调用startMonitoring的getData。

startMonitoring() {
connectivity.startMonitoring((newConnectionType: number) => {
switch (newConnectionType) {
case connectivity.connectionType.none:{
console.log("No Connection!");
break;
}
case connectivity.connectionType.wifi:{
while(!this.isQueueEmpty()){
this.service.getData().subscribe((res)=> {console.log(res);});
}
console.log("Connection type using WiFi.");
break;
}                    
}
});

或者将processQueue转换为promise。只是一个提示,您的startMonitoring可能是一个拦截器:(

没有特定于服务与组件的问题。然而,问题在于调用HTTP API的方式。每次API调用后,都会返回observable,但方法没有时间执行。队列仅在订阅方法执行时弹出,而订阅方法从未执行。因此,即使调用是正确的,所有调用都会添加到PENDING中。

最新更新