我很难让一个简单的http呼叫开始工作。
我来自angularjs和AFAIK .then = .subscribe
所以我的问题是,我不能在我的login.component.ts中使用.subscribe而不是在我的auth服务中使用它,我也想获得Http状态。
auth.service
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}
export class AuthService {
constructor(private http: HttpClient) { }
mainUrl = 'http://localhost:4000/api/v1';
signIn(user: User) { // this is working fine, BUT I want to use .subscribe in login component
const url = `${this.mainUrl}/sign_in`;
return this.http.post<any>(url, user, httpOptions)
.subscribe(
(data) => {
console.log(data); // I want to get the 200 status not only the jwt token
return data;
},
(error) => console.log(error)
)
}
}login.component.ts
this.authService.signIn(userData); // how to use .subscribe here ? 更新:
这是我的angularjs代码的样子。
auth.service
function signIn(data) {
return $http.post(url + '/api/v1/sign_in_2', data)
.then(success)
.catch(fail);
function success(response) {
setToken(response.data.jwt);
return response;
}
function fail(e) {
return e;
}
}login.ctrl
vm.doLogin = function (userData) {
vm.spinner = true;
elixirAuthServices.signIn(userData).then(function (data) {
if (data.status === 200 || data.status === 201) {
elixirAuthServices.getUser().then(function (data) {
vm.user_id = data.data.id;
redirect();
});
} else {
vm.authenticateErr = true;
vm.spinner = false;
logger.info('Username or Password is incorrect');
}
});
};发布于 2019-03-06 02:19:48
您已经接近了,但是您应该对您的service.ts进行以下修改。不要忘记导入所需的RxJS操作符,如管道,并映射,如果您打算使用它们。
import { catchError, map, tap } from 'rxjs/operators';
signIn(user: User) {
const url = `${this.mainUrl}/sign_in`;
return this.http.post<any>(url, user, httpOptions).pipe(
// additional operations before returning observable
//map(data => ....),
//catchError(this.handleError('signIn', []))
);
}在您的component.ts上,您可以将订阅所观察到的内容返回给它。订阅方法处理next、error和complete的回调。
this.authService.signIn(userData).subscribe(success => {
// next
// .. handle successful response (status code 200)
}, error => {
// handle errors
console.log(error.status)
}, () => {
// complete
})编辑:如果要读取完整的响应体,可以使用observe选项
this.http.post<any>(url, user, { observe: 'response' }).pipe(
//additional operations before returning observable
//tap(response => console.log(response.status)
//map(response => response.body),
//catchError(this.handleError('signIn', []))
);发布于 2019-03-06 02:13:36
subscribe()和then()方法是不一样的。您只需返回http.post()的结果,即Observable对象,然后在组件中订阅可观察到的内容。
如果要捕获错误,请在服务中链接catch()方法。
auth.service.ts
signIn(user: User) {
const url = `${this.mainUrl}/sign_in`;
return this.http.post<any>(url, user, httpOptions)
.pipe(
tap(val => console.log(val)), // do what you want with val
catchError(error => console.log(error))
)
}login.component.ts
this.authService.signIn.subscribe(userData => {
// do what did in login.ctrl
});发布于 2019-03-06 03:53:12
RxJ可观测到
signIn(user){
return this.httpClient.post<any>(url, user, option)
.pipe(
map(res => res),
retry(1),
catchError(err => throwError(err))
)
}然后,您可以在LoginComponent订阅响应和捕获错误。
this.authService.signIn(userData).subscribe(res => {
// do something here
}, err => {
// catch err here
}
)https://stackoverflow.com/questions/55014365
复制相似问题