所以,我正在构建一个 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 {}
什么是最好的方法:
- 继续使用功能模块路由,并且
- 将路由保护应用于用户功能模块路由而不复制 canActivate 保护?有什么方法可以将它从根路由模块传递给用户功能模块路由?
谢谢,
伊拉克利斯