【发布时间】:2020-04-16 10:04:09
【问题描述】:
我正在用 Angular 构建身份验证。登录后,应重定向用户。我的问题是 router.navigate() 函数似乎不起作用...... 这是不起作用的代码:
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;
}
);
}
console.log 确实会在登录成功时显示,但它不会导航到 chore/show 路由。
应用路线:
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 模块路由:
const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent},
{ path: '', redirectTo: 'login', pathMatch: "full"}
];
以及杂务模块路由:
const routes: Routes = [
{ path: 'show', component: ShowChoreComponent},
{ path: '', redirectTo: 'show', pathMatch: 'full' },
];
由于某种原因,我无法在登录后重定向它。有什么建议么? 编辑:添加了应用程序路线。代码在https://github.com/tomgelmers/stack
【问题讨论】:
-
开发者控制台有错误吗?
-
@Çağrı 不,它只显示 console.log(this.router)。
-
你可以为你的代码创建堆栈闪电战
-
你去家务模块的路线在哪里?
-
@SayoojVR 我忘了添加它们,它们现在在帖子中。
标签: javascript angular typescript routing