简体   繁体   English

观测值返回类型

[英]Observables return type

Im sending my request to API and parse it with map function: 我将请求发送到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});
}

As a result I get boolean value and use it in subscribe function: 结果,我得到布尔值并在订阅函数中使用它:

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

Problem is that in doLogin's subscriber TypeScript says that result is Response instead of boolean - how to fix it? 问题是在doLogin的订户TypeScript中说结果是Response而不是boolean如何解决?

That's cause of your function prototypes: 这是您的函数原型的原因:

post(url: string, params): Observable< Response > post(URL:字符串,参数):Observable < Response >

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

They should be: 他们应该是:

Observable< boolean > Observable < 布尔值 >

Do it like this: 像这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM