所以,我正在构建一个Angular 4应用程序,我希望所有路由都受到保护(当然除了登录路由)。我尝试使用功能模块和功能模块路由。所以,想象一下这样的事情:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AuthGuardService } from './auth/auth-guard.service';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { ProtectedRouteComponent } from './protected-route/protected-route.component';
const routes: Routes = [
{ path: 'login', component: LoginComponent }, // no auth guard for login
{
path: '',
canActivate: [AuthGuardService],
children: [
{
path: '',
children: [
{ path: 'register', component: RegisterComponent },
{ path: 'protected', component: ProtectedRouteComponent }
]
}
]
}
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
,对于根模块,然后是用户功能模块路由配置,如:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { UsersComponent } from './users.component';
const routes: Routes = [
{ path: 'users', component: UsersComponent }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class UsersRoutingModule {}
最好的方法是什么:
谢谢,
伊拿克里斯
答案 0 :(得分:0)
寻找相同的东西,我找到的解决方案涉及以下步骤:
{
path: '',
canActivate: [AuthGuardService],
children: [
{
path: '',
children: [
{ path: 'register', component: RegisterComponent },
{ path: 'protected', component: ProtectedRouteComponent }
]
},
{
path: '',
children: () => UsersModule
}
]
}
要考虑的一件事:
/users
条路线之一)