【发布时间】:2019-06-30 19:23:10
【问题描述】:
我有一个 LoginComponent(用于凭据的表单)和一个 LoginService,用于使用 httpClient 调用我的 api。
通常在我的服务中我会返回调用,以便我可以在我的组件中订阅它。
例如:
doSomething() {
return this.http.post<any>(...);
}
这里的问题是我需要在我的组件和服务中订阅调用,因为我想在那里处理存储。
我在教程中找到了这个解决方案,但我认为这不是最合适的方法,也许使用 rxjs 管道有更好的解决方案。
授权服务:
login(userName: string, password: string, onSuccess: () => void, onError: (error) => void) {
this.http.post("https://localhost:5001/api/auth/login", {userName: userName, password: password})
.subscribe(response => {
let token = (<any>response).token;
localStorage.setItem("jwt", token);
this.readUserFromLocalStorage();
onSuccess();
}, err => {
onError(err);
});
}
在我的组件内部
login() {
this.auth.login(this.userName, this.password,
() => this.router.navigateByUrl('/'),
(error) => this.invalidLogin = true
);
}
【问题讨论】:
-
使用github.com/auth0/angular-jwt,为什么要推出自己的解决方案?
标签: angular rxjs httpclient