简体   繁体   English

为什么 Angular router.navigate() 无法导航?

[英]Why is Angular router.navigate() not navigating?

I'm building an authentication in angular.我正在 angular 中构建身份验证。 When logged in, the user should be redirected.登录后,应重定向用户。 My problem is that the router.navigate() function doesn't seem to work... This is the code that doesn't work:我的问题是 router.navigate() function 似乎不起作用......这是不起作用的代码:

async login() {
 if(!this.email || !this.password) {
   return;
 }
 this.showSpinner = true;
 this.authService.login(this.email, this.password).then(
   (data) => {
     this.router.navigate(['/chore/show']);
     console.log(this.router);
   },
   (error) => {
     this.error = error.message;
     this.showSpinner = false;
   }
 );
}

The console.log does show when the login succeeds, but it won't navigate to the chore/show route.当登录成功时,console.log 会显示,但它不会导航到 chore/show 路由。

app routes:应用路线:

const routes: Routes = [
 { path: '', redirectTo: 'auth', pathMatch: 'full' },
 { path: 'auth', loadChildren: './modules/auth/auth.module#AuthModule' },
 { path: 'chore', loadChildren: 
 './modules/chore/chore.module#ChoreModule', canActivate: [AuthGuard] }
];

auth module routes:认证模块路线:

const routes: Routes = [
 { path: 'login', component: LoginComponent },
 { path: 'register', component: RegisterComponent},
 { path: '', redirectTo: 'login', pathMatch: "full"}
];

and the chore module routing:和家务模块路由:

const routes: Routes = [
 { path: 'show', component: ShowChoreComponent},
 { path: '', redirectTo: 'show', pathMatch: 'full' },
];

For some reason i can't get it to redirect after login.由于某种原因,我无法在登录后重定向它。 Any suggestions?有什么建议么? EDIT: Added app routes.编辑:添加了应用程序路线。 Code is on https://github.com/tomgelmers/stack代码在https://github.com/tomgelmers/stack

Checking your Code on Github, i would say the problem is that the navigation gets blocked by your AuthGuard.检查 Github 上的代码,我会说问题是导航被您的 AuthGuard 阻止。

The AuthGuard is calling the isLoggedIn function of your authService, which checks localstorage for userdata, while the authState subscriber is still writing to the localstorage. AuthGuard 正在调用您的 authService 的 isLoggedIn function,它检查本地存储中的用户数据,而 authState 订阅者仍在写入本地存储。

This kind of thing can either work or don't work, it is pretty much luck and how fast the localstorage api works.这种事情可以工作也可以不工作,这很幸运,本地存储 api 工作的速度有多快。

I recommend you check in the isLoggedIn function the variable user of your AuthService instead of localstorage.我建议您在 isLoggedIn function 中检查 AuthService 的变量用户而不是本地存储。 You can check localstorage in case the user variable is undefined.如果用户变量未定义,您可以检查 localstorage。

That should be relatively save, however i don't know what the timing is between the login function returning and the authState subscriber firing.那应该相对节省,但是我不知道登录 function 返回和 authState 订阅者触发之间的时间。 You might want to set the user variable in the login function.您可能希望在登录 function 中设置用户变量。 the signInWithEmailAndPassword function does return a Promise with Credentials that has a user property on it. signInWithEmailAndPassword function 确实返回了一个 Promise ,其凭据上有一个用户属性。

You could change the login function to您可以将登录 function 更改为

async login(email: string, password: string) {
  return this.afAuth.signInWithEmailAndPassword(email, password).then(userData => {
     this.user = userData.user
     return userData
  }
}

And the isLoggedIn function to而 isLoggedIn function 到

get isLoggedIn(): boolean {
   if(this.user) return !!this.user
   const  user  =  JSON.parse(localStorage.getItem('user'));
   return  user  !==  null;
}

Oh and for completions sake, you should probably set the user variable to undefined or null in your logout function.哦,为了完成,您可能应该在注销 function 中将用户变量设置为未定义或 null。 Even if the user logs out, the user data still hangs around in memory即使用户注销,用户数据仍然在 memory 中徘徊

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM