我将请求发送到API并使用map函数解析它:
//part of service
post(url: string, params): Observable<Response> {
let requestUrl: string = this.apiUrl + url;
return this.http.post(requestUrl, params)
.map(response => response.json());
}
//part of other service
doLogin(login, haslo): Observable<Response> {
return this.apiService.post('auth/login/', {login: login, haslo: haslo});
}
结果我获得了布尔值并在订阅函数中使用它:
this.authService.doLogin(this.model.login, this.model.haslo)
.subscribe(result => {
//result is boolean - not Response
this.authService.loggedIn = result;
this.result = result
});
问题是doLogin的订阅者TypeScript说结果是Response
而不是boolean
- 如何解决?
答案 0 :(得分:4)
你的功能原型的原因:
post(url:string,params):Observable&lt; Response &gt;
doLogin(login,haslo):可观察&lt; 响应&gt;
他们应该是:
可观察&LT;的布尔强>&GT;
这样做:
//part of service
post(url: string, params): Observable<any> {
let requestUrl: string = this.apiUrl + url;
return this.http.post(requestUrl, params)
.map(response => response.json());
}
//part of other service
doLogin(login, haslo): Observable<boolean> {
return this.apiService.post('auth/login/', {login: login, haslo: haslo})
.map(result => result == true /* or any check here and return an BOOLEAN !!*/);
}