Angular 使用typescript,什么是一个好的可观察类型,可以最好地表示没有内容的HTTP响应?
使用typescript、Angular 6+和rxjs,如何最好地向调用者表示HTTP响应内容为空(内容长度=0) 如果不在方法调用中指定类型(Angular 使用typescript,什么是一个好的可观察类型,可以最好地表示没有内容的HTTP响应?,angular,typescript,http,rxjs,Angular,Typescript,Http,Rxjs,使用typescript、Angular 6+和rxjs,如何最好地向调用者表示HTTP响应内容为空(内容长度=0) 如果不在方法调用中指定类型(get,post等),则返回一个可观察的,这对于响应内容是误导性的,因为可能会尝试使用给定的对象: let-response:Observable=httpClient.post(“…”); 订阅(o=>…); 在这种情况下,o始终为空,但这不是显式的,也不是由编译器检查的 更好的解决方案是返回Observable,但我发现语义不明确,因为null也
get
,post
等),则返回一个可观察的
,这对于响应内容是误导性的,因为可能会尝试使用给定的对象:
let-response:Observable=httpClient.post(“…”);
订阅(o=>…);
在这种情况下,o
始终为空,但这不是显式的,也不是由编译器检查的
更好的解决方案是返回
Observable
,但我发现语义不明确,因为null也可以指null数据(例如,没有电话号码的客户的phoneNumber属性设置为null)。您可以使用Observable.empty()
或
empty()
// Add a new comment
addComment (body: Object): Observable<Comment[]> {
let bodyString = JSON.stringify(body); // Stringify payload
let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
let options = new RequestOptions({ headers: headers }); // Create a request option
return this.http.post(this.commentsUrl, body, options) // ...using post request
.map((res:Response) => res.json()) // ...and calling .json() on the response to return data
.catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if any
}
// Update a comment
updateComment (body: Object): Observable<Comment[]> {
let bodyString = JSON.stringify(body); // Stringify payload
let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
let options = new RequestOptions({ headers: headers }); // Create a request option
return this.http.put(`${this.commentsUrl}/${body['id']}`, body, options) // ...using put request
.map((res:Response) => res.json()) // ...and calling .json() on the response to return data
.catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if any
}
// Delete a comment
removeComment (id:string): Observable<Comment[]> {
return this.http.delete(`${this.commentsUrl}/${id}`) // ...using put request
.map((res:Response) => res.json()) // ...and calling .json() on the response to return data
.catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if any
}
getConfigResponse(): Observable<HttpResponse<Config>> {
return this.http.get<Config>(
this.configUrl, { observe: 'response' });
}
addHero (hero: Hero): Observable<Hero> {
return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
.pipe(
catchError(this.handleError('addHero', hero))
);
}
您所寻找的实际上是存在的,它的正确类型是Observable
。正如您所说的,null
是一个规则值,可以由任何可观察的对象生成。在RxJS 6中,这是由empty()
表示的,它与任何其他可观察对象一样,但不发出任何next
值,只发送complete
信号
import { empty, EMPTY } from 'rxjs';
empty().subscribe(...) // will receive just the `complete` notification
// or you can use EMPTY
EMPTY.subscribe(...)
顺便说一句,您还可以使用ignoreElements()
操作符将任何可观察对象转换为Observable
还有never()
和never
可观察对象,但不要将它们误认为empty()
,因为它们不会发出任何东西(甚至不会发出complete
通知)。请检查:这不是完美的,因为它需要更改订阅方法(使用complete
回调而不是next
)。但我同意empty
是传达包含空内容的已完成HTTP请求含义的最佳操作符。@Clément您可以使用订阅({complete:()=>…})
Observable
在技术上是可行的,但它的语义很难推理:它发出什么东西吗?值“void”在什么意义上?empty()
是好的,但正如我对@Sunil Singh所说的,subscribe方法的使用根据是否有内容而改变,这太糟糕了(有时使用next
,有时使用complete
)。嗯,它必须更改。如何区分有效值和不想发出任何信息的情况?这就是complete
和error
通知的作用。当然,您可以使用null
,但似乎您希望避免这种情况。void
类型是表示无(如数据库中的NULL
)。然后observatable
是发出next
类型T
项的可观察对象。因此observatable
是不发出任何信息的可观察对象。