Observable 返回类型

pie*_*nik 2 observable rxjs angular

我将我的请求发送到 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});
}
Run Code Online (Sandbox Code Playgroud)

结果我得到布尔值并在订阅函数中使用它:

this.authService.doLogin(this.model.login, this.model.haslo)
    .subscribe(result => {
        //result is boolean - not Response
        this.authService.loggedIn = result;
        this.result = result
    });
Run Code Online (Sandbox Code Playgroud)

问题是在 doLogin 的订阅者 TypeScript 中说结果Response不是boolean- 如何修复它?

mxi*_*xii 5

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

post(url: string, params): Observable<响应>

doLogin(login, haslo): Observable< Response >

他们应该是:

可观察的<布尔值>

像这样做:

//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 !!*/);
}
Run Code Online (Sandbox Code Playgroud)