如何 return observable of string
How to return observable of string
有一个 api 方法,return 是一个简单的字符串。我正在我的 angular 服务中实现一个方法,该方法需要 return 一个可观察的
简单字符串不会直接映射到可观察对象中。我如何 return 使用 api 的简单字符串结果的可观察对象?
public setLineItemListApproval(lines : OrderLinesModel) : Observable<string>{
let actionUrl = this.apiBaseURL + '/mycontroller/actionmethod1';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.httpClient.post<string>(actionUrl, lines, httpOptions).pipe(catchError(this.handleError));
}
你需要
- 在
options
参数中包含 responseType: 'text
- 在调用中内联选项
- 使用
post
的 non-generic 重载作为 responseType: 'text'
选项现在指示使用哪个选项以及 Observable
响应中的 return 类型
public setLineItemListApproval(lines : OrderLinesModel) : Observable<string>{
let actionUrl = this.apiBaseURL + '/mycontroller/actionmethod1';
return this.httpClient.post(actionUrl, lines, {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
responseType: 'text'
}).pipe(catchError(this.handleError));
}
为了完成这个:
- 在选项参数中添加 responseType: 'text'
这个简单的更改应该有效
有一个 api 方法,return 是一个简单的字符串。我正在我的 angular 服务中实现一个方法,该方法需要 return 一个可观察的
简单字符串不会直接映射到可观察对象中。我如何 return 使用 api 的简单字符串结果的可观察对象?
public setLineItemListApproval(lines : OrderLinesModel) : Observable<string>{
let actionUrl = this.apiBaseURL + '/mycontroller/actionmethod1';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.httpClient.post<string>(actionUrl, lines, httpOptions).pipe(catchError(this.handleError));
}
你需要
- 在
options
参数中包含responseType: 'text
- 在调用中内联选项
- 使用
post
的 non-generic 重载作为responseType: 'text'
选项现在指示使用哪个选项以及Observable
响应中的 return 类型
public setLineItemListApproval(lines : OrderLinesModel) : Observable<string>{
let actionUrl = this.apiBaseURL + '/mycontroller/actionmethod1';
return this.httpClient.post(actionUrl, lines, {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
responseType: 'text'
}).pipe(catchError(this.handleError));
}
为了完成这个:
- 在选项参数中添加 responseType: 'text'
这个简单的更改应该有效