请帮帮我如何在订阅中调用函数。在下面的代码中,如果用户成功登录,他会根据条件重定向到其他页面
Login(UserName: string, Password: string) {
var token = this.LoginSer.access_token;
if (UserName != null && Password != null) {
return this.LoginSer.LoginDetails(UserName, Password)
.subscribe(data => {
this.isAuthenticated = data, alert('successfullyyy...'), this.LogedIn
}, error => alert('Given Details Not Matching...'))
}
我实现了一些方法这里基于条件的路由我在订阅中调用此方法为 This.LogedIn
LogedIn():void {
if (localStorage.getItem('currentUser')) {
var cur = JSON.parse(localStorage.getItem('currentUser'));
var user = cur.username;
if (user == "admin") {
this.router.navigate[("Admin/Add")]
}
}
答案 0 :(得分:3)
我认为你需要这个,功能应该被称为 this.LogedIn();
if (UserName != null && Password != null) {
return this.LoginSer.LoginDetails(UserName, Password)
.subscribe(data => {
this.isAuthenticated = data;
alert('successfullyyy...');
this.LogedIn();
}, error => alert('Given Details Not Matching...'))
}