gpt4 book ai didi

angular - 可观察返回类型

转载 作者:行者123 更新时间:2023-12-02 20:57:52 25 4
gpt4 key购买 nike

我将我的请求发送到 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});
}

结果我得到一个 bool 值并在订阅函数中使用它:

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

最佳答案

那是你的函数原型(prototype)的原因:

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

doLogin(login, haslo): Observable<Response>>

它们应该是:

可观察< bool 值>

这样做:

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

关于angular - 可观察返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39614474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com