[英]How to check the response from an Observable and display alerts for different responses in Angular4 with Rxjs?
I have this service: 我有这项服务:
return this.http.post(this.loginUrl, bodyString, options)
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error || 'Server error'));
with the function: auth(body: Object) : Observable<Response>
具有以下功能:
auth(body: Object) : Observable<Response>
And then I'm subscribing in the component: 然后,我订阅了该组件:
this.loginService.auth(value).subscribe(
result => {
swal('Error', result, 'warning')
},
err => {
// Log errors if any
console.log(err);
});
How can I handle the value of the result
and where? 如何处理
result
的值以及在哪里? I want to display it based on his different values. 我想根据他的不同值来显示它。 You cannot check in the subscribe with
if
and ==
. 您不能使用
if
和==
签入订阅。
You can check the value of the result returned in the subscription. 您可以检查预订中返回的结果的值。 I'm going to assume result is a simple string, but it could actually be anything.
我将假设结果是一个简单的字符串,但实际上可以是任何东西。 Check what's being returned in your result object and see whats most appropriate.
检查结果对象中返回的内容,然后查看最合适的内容。
this.loginService.auth(value).subscribe((result) => {
if (result === 'ok') {
// its all good
}
if (result === 'bad') {
// its bad
}
}, (err) => {
// Log errors if any
console.log(err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.