Angular 2.0 Router.navigate 登录后不重定向
Angular 2.0 Router.navigate not redirecting after login
我有一个连接到 firebase 的简单应用程序。在登录时,我只想将用户重定向到他们的个人资料页面。但是,由于某些奇怪的原因,重定向没有发生。
我看过 here 这听起来像是一个类似的问题,但不幸的是,那里没有解决方案。
将重定向更改为另一个页面(没有授权保护的页面)似乎可行,所以我想问题就在那里,我只是不知道如何解决它。
这是我的代码,一旦用户登录,它就会调用我的服务:
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);
这是我的 auth.guard.ts
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.authService.isAuthenticated().first();
}
}
完整项目在 github,以防您需要检查其他配置。
您的 auth
或 signInWithEmailAndPassword
方法可能 returns Observable
所以它们被延迟了。路由器导航立即发生。尝试将其包含在 subscribe()
调用中。
终于明白了!我很接近,但正如 Jan 在他的回答中指出的那样,在注册和身份验证建立之前,导航立即发生。通过点击 .then
,我可以在创建用户后执行操作。
firebase.auth().createUserWithEmailAndPassword(user.email, user.password)
.then(success => {
this.router.navigate(['/profile']);
})
.catch(function (error) {...}
我有一个连接到 firebase 的简单应用程序。在登录时,我只想将用户重定向到他们的个人资料页面。但是,由于某些奇怪的原因,重定向没有发生。
我看过 here 这听起来像是一个类似的问题,但不幸的是,那里没有解决方案。
将重定向更改为另一个页面(没有授权保护的页面)似乎可行,所以我想问题就在那里,我只是不知道如何解决它。
这是我的代码,一旦用户登录,它就会调用我的服务:
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);
这是我的 auth.guard.ts
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.authService.isAuthenticated().first();
}
}
完整项目在 github,以防您需要检查其他配置。
您的 auth
或 signInWithEmailAndPassword
方法可能 returns Observable
所以它们被延迟了。路由器导航立即发生。尝试将其包含在 subscribe()
调用中。
终于明白了!我很接近,但正如 Jan 在他的回答中指出的那样,在注册和身份验证建立之前,导航立即发生。通过点击 .then
,我可以在创建用户后执行操作。
firebase.auth().createUserWithEmailAndPassword(user.email, user.password)
.then(success => {
this.router.navigate(['/profile']);
})
.catch(function (error) {...}