【发布时间】:2017-08-16 11:59:29
【问题描述】:
我在我的应用程序的登录组件中创建了一个登录功能
在 LoginComponent.ts 中
onSubmit(loginForm: NgForm): void {
if(loginForm.valid) {
this.authService.login(loginForm.value)
.then(data => {
if(data.status && data.status == 'first_login') {
console.log('in first_login');
this.router.navigate(['/first-login']);
} else {
localStorage.setItem('user', JSON.stringify(data));
this.router.navigate(['/']);
}
});
}
路由配置:
{
path: 'login',
component: LoginComponent
},
{
path: 'not-found',
loadChildren: './not-found/not-found.module#NotFoundModule'
},
{
path: 'first-login',
loadChildren: './first-login/first-login.module#FirstLoginModule'
},
{
path: '**',
redirectTo: 'not-found'
},
在从 authService 获取数据的这个函数中,我可以在控制台中看到日志 first_login 但路由器没有导航到所需的 url。
【问题讨论】:
-
你的路由器是如何定义的?是像
export const AppRoutes: any = [ { path: "/first-login", component: FirstLoginComponent } ];一样定义的 -
我的路由器定义在一个名为 routing.ts
{ path: 'login', component: LoginComponent }, { path: 'not-found', loadChildren: './not-found/not-found.module#NotFoundModule' }, { path: 'first-login', loadChildren: './first-login/first-login.module#FirstLoginModule'}, { path: '**', redirectTo: 'not-found' },的文件中