Angular 的新手,我找不到任何可以帮助我的东西,非常感谢你的帮助。
宁可不用rxjs来处理这种情况; IE。 .pipe( map (...))
api 响应:
status(200) { token: "..."} --or-- status(400)
login(credentials) {
return this.http.post('.../api/authenticate', credentials)
.subscribe(
response => {
localStorage.setItem('token', response.token);
return true;
},
() => {
return false;
});
}
如何简单地返回 bool 值?
最佳答案
不,这不是正确的实现。当你点击订阅方法时,它会自动在后台点击 API 而不会卡住脚本(通过不同的线程)。您可以使用回调方法来实现您想要的结果,查看下面提到的逻辑,
您的登录函数应该看起来像这样,
login(credentials, callback) {
return this.http.post('.../api/authenticate', credentials)
.subscribe(
(response: any) => {
callback(response.status);
localStorage.setItem('token', response.token);
});
}
调用这个函数这个这个,
login(credentials, (status) => {
console.log(status);
});
关于angular - 我怎样才能简单地返回 bool 值 angular 6 .subscribe(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50849943/