Observables return 类型

Observables return type

我正在将我的请求发送到 API 并使用映射函数对其进行解析:

//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 - 我该如何解决这个问题?

这是你的函数原型的原因:

post(url: string, params): Observable<Response>

doLogin(login, haslo): 可观察<响应>

它们应该是:

可观察<布尔值>

这样做:

//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 !!*/);
}