【发布时间】:2019-04-30 21:04:21
【问题描述】:
google.com 是主页。我需要两条不同的主页路线。第一个路由在用户未登录 (Auth) 时有效,第二个路由在用户登录时有效(Dashboard)。
困难的部分是,当身份验证页面和仪表板页面都在一个主页上工作时,而不是不同的 URL。
注意:这两个模块都有多个组件。 Auth 有 (login - signup - reset) 组件,Dashboard 有 (index - users - posts) 组件。
const routes: Routes = [
{
path: 'index',
loadChildren: './modules/index/index.module#IndexModule'
},
{
path: 'error',
loadChildren: './modules/error/error.module#ErrorModule'
},
// Fallback when no prior routes is matched
{ path: '**', redirectTo: 'not-found', pathMatch: 'full' }
];
@NgModule({
imports: [RouterModule.forRoot(routes, { useHash: false })],
exports: [RouterModule],
providers: []
})
export class AppRoutingModule { }
const routes: Routes = [
{
path: '',
children: [
{
path: '',
loadChildren: './modules/home/home.module#HomeModule',
canLoad: [AuthGuard],
},
{
path: 'auth',
loadChildren: './modules/auth/auth.module#AuthModule',
canLoad: [NoAuthGuard]
},
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class IndexRoutingModule { }
const routes: Routes = [
{
path: '',
component: HomeLayoutComponent,
pathMatch: 'full',
children: [
{
path: '',
redirectTo: 'dashboard',
pathMatch: 'full'
},
{
path: 'dashboard',
component: DashboardComponent,
data: { title: 'Dashboard' }
},
]
},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class HomeRoutingModule { }
const routes: Routes = [
{
path: '',
component: AuthLayoutComponent,
pathMatch: 'full',
children: [
{
path: '',
redirectTo: 'login',
pathMatch: 'full'
},
{
path: 'login',
component: LoginComponent,
data: { title: 'Sign In To Admin' }
},
]
}
];
@NgModule({
imports: [
RouterModule.forChild(routes)
],
exports: [
RouterModule
]
})
export class AuthRoutingModule { }
问题只在于路由,而不是守卫,什么都没有。
【问题讨论】: