Has*_*san 1 typescript angular
我有一个用于身份验证的私有方法。然后我有另一个方法(公共),我在我的组件中调用它login来进行实际登录。我希望能够订阅login实际上订阅私有authentication方法的方法,以便我可以在登录时显示加载和其他消息,而每个视图的执行方式都不同。是否可以?
身份验证方法:
private userAuthenticate( email: string, password: string ) {
return this.httpPost(`${this.baseApiUrl}/auth?format=json&provider=login`, {userName: email, password: password}).subscribe(
res => this.saveJwt(res.bearerToken),
err => this.logError(err),
() => console.log("Authentication done.")
);
}
Run Code Online (Sandbox Code Playgroud)
登录方式:
login( email: string, password: string ) {
this.logout();
return this.userAuthenticate(email, password);
}
Run Code Online (Sandbox Code Playgroud)
我想订阅登录方法,以便我可以控制我的加载程序和错误消息等。请帮忙。
您不能订阅Subscription(由 返回subscribe()),您只能订阅Observable。
要获得Observable使用map()而不是subscribe(),则 的调用者login()可以进行订阅
private userAuthenticate( email: string, password: string ) {
return this.httpPost(`${this.baseApiUrl}/auth?format=json&provider=login`, {userName: email, password: password}).map(
res => this.saveJwt(res.bearerToken),
);
Run Code Online (Sandbox Code Playgroud)
对于其他回调,如果您确实需要它们,可以使用catchandfinally运算符。
| 归档时间: |
|
| 查看次数: |
3394 次 |
| 最近记录: |