我在得到一个简单的HTTP调用的工作问题。
我从angularjs来了,据我所知.then
= .subscribe
因此,这里是我的问题,我不能使用.subscribe
我的身份验证服务 ,而是在我的login.component.ts使用它,我想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');
}
});
};