Redirect Angular 2.0路由器。登录后导航不重定向
我有一个连接到firebase的简单应用程序。登录时,我只想将用户重定向到他们的个人资料页面。然而,由于一些奇怪的原因,重定向没有发生 我看了一下,这听起来像是一个类似的问题,但不幸的是,没有解决办法 将重定向更改为另一个页面(一个没有auth guard的页面)似乎是可行的,所以我猜问题就在那里,我只是不知道如何修复它 这是我的代码,一旦用户登录,它就会调用我的服务:Redirect Angular 2.0路由器。登录后导航不重定向,redirect,angular,typescript,angular-routing,Redirect,Angular,Typescript,Angular Routing,我有一个连接到firebase的简单应用程序。登录时,我只想将用户重定向到他们的个人资料页面。然而,由于一些奇怪的原因,重定向没有发生 我看了一下,这听起来像是一个类似的问题,但不幸的是,没有解决办法 将重定向更改为另一个页面(一个没有auth guard的页面)似乎是可行的,所以我猜问题就在那里,我只是不知道如何修复它 这是我的代码,一旦用户登录,它就会调用我的服务: onLogin() { this.authService.signinUser(this.loginForm.valu
onLogin() {
this.authService.signinUser(this.loginForm.value);
}
和我的auth.service.ts
:
public signinUser(user: User) {
firebase.auth().signInWithEmailAndPassword(user.email, user.password)
.catch(function (error) {
// Handle Errors here.
let errorCode = error.code;
let errorMessage = error.message;
// todo
console.log(errorCode);
console.log(errorMessage);
});
this.router.navigate(['/profile']); //not working
//this.router.navigate(['/']); //working
}
这是我的app.routing.ts
const APP_ROUTES: Routes = [
{ path: '', component: HomeComponent },
{ path: 'login', component: LoginComponent },
{ path: 'signup', component: SignupComponent },
{ path: 'profile', component: ProfileComponent, canActivate: [AuthGuard] },
];
export const routing = RouterModule.forRoot(APP_ROUTES);
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.authService.isAuthenticated().first();
}
}
这是我的auth.guard.ts
const APP_ROUTES: Routes = [
{ path: '', component: HomeComponent },
{ path: 'login', component: LoginComponent },
{ path: 'signup', component: SignupComponent },
{ path: 'profile', component: ProfileComponent, canActivate: [AuthGuard] },
];
export const routing = RouterModule.forRoot(APP_ROUTES);
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.authService.isAuthenticated().first();
}
}
@Injectable()
导出类AuthGuard实现了CanActivate{
构造函数(私有authService:authService){}
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察的布尔值{
返回此.authService.isAuthenticated().first();
}
}
完整项目已打开,以防您需要检查其他配置 您的
auth
或使用Email和Password登录
方法可能会返回可观察
,因此它们会被延迟。路由器导航立即发生。尝试将其包含到subscribe()
调用中。您的auth
或使用EmailandPassword登录方法可能会返回可观察的
,因此它们会延迟。路由器导航立即发生。试着把它包含到subscribe()
调用中。终于找到了答案!我很接近,但正如简在回答中指出的那样,导航立即发生,在注册和认证建立之前。通过点击。然后
我可以在创建用户后执行该操作
firebase.auth().createUserWithEmailAndPassword(user.email, user.password)
.then(success => {
this.router.navigate(['/profile']);
})
.catch(function (error) {...}
终于明白了!我很接近,但正如简在回答中指出的那样,导航立即发生,在注册和认证建立之前。通过点击。然后
我可以在创建用户后执行该操作
firebase.auth().createUserWithEmailAndPassword(user.email, user.password)
.then(success => {
this.router.navigate(['/profile']);
})
.catch(function (error) {...}
控制台有错误吗?@SefaÜmitOray绝对没有,控制台是干净的。唯一有点奇怪的是,如果我重新单击“登录”按钮,它确实会重定向,但只会在第二秒attempt@SefaÜmitOray当您登录时会返回什么canActivate(route:ActivatedRouteSnapshot,state:routerstatesnashot):Observable | boolean{返回this.authService.isAuthenticated().first();}
控制台上有任何错误吗?@SefaÜmitOray完全没有,控制台是干净的。唯一有点奇怪的是,如果我重新单击“登录”按钮,它确实会重定向,但只会在第二秒attempt@SefaÜmitOray当您登录时会返回什么<代码>canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察的|布尔{返回this.authService.isAuthenticated().first();}