【发布时间】:2017-12-13 23:17:15
【问题描述】:
我正在尝试在http-service 文件中触发错误回调,如果存在任何错误,我将从本地服务调用数据。我将其返回到名为 app-config-service 的服务文件。
http-service.ts 的代码:
import { Injectable } from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
import { MockService } from './mock-service';
@Injectable()
export class HttpRestService {
constructor(private http:Http,mockService:MockService ) {
}
get(pUrl) {
return this.http.get(pUrl).subscribe(
function(response) { res => response.json()},
function(error) { res => this.mockService.userMethods(pUrl) },
function() { console.log("the subscription is completed")}
);
}
post(pUrl, pData) {
return this.http.post(pUrl, pData).map(res => res.json());
}
}
app-config-service.ts 的代码:
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { HttpRestService } from './http-service';
@Injectable()
export class AppConfigService {
constructor(private httpRestService: HttpRestService) {
}
testAPIMethod(pUrl) {
return this.httpRestService.get(pUrl).map(res => res);
}
}
据我所知,subscribe 用于操作接收到的结果,map 用于直接复制数据而不做任何更改。
所以我在这些情况下使用了订阅和映射。
我不知道为什么会触发错误,因为我是打字稿的新手。
错误如下:
typescript: E:/ActiveProjects/IonBase/src/services/app-config-service.ts, line: 14
Property 'map' does not exist on type 'Subscription'.
L13: testAPIMethod(pUrl) {
L14: return this.httpRestService.get(pUrl).map(res => res);
我能做些什么来解决这个问题?
【问题讨论】:
标签: angular typescript angular-services