默认情况下,角度保护路由

jen*_*gar 2 angular

我有一个应用程序,我的大多数路线需要保护(登录).是否可以添加默认路由防护和应该打开的"白名单"路由?

Vit*_*vzh 5

我通常做的是为应用程序的授权部分创建一个父路由,并在其上设置路由保护.事实上,这条路线<router-outlet></router-outlet>在其模板内部只有基本组件,但在该路线上击中路线保护之前,无法访问子路线.请参阅下面的示例.

const routes: Routes = [
    { path: '',  redirectTo: 'app/books', pathMatch: 'full' },
    { path: 'app', component: MainComponent, canActivate: [AuthGuard], children: [
        {path: 'books', component: BooksComponent },
        {path: 'cars', component: CarsComponent },
        {path: 'trees', component: TreesComponent }
    ]},
    { path: 'login', component: LoginComponent, canActivate: [NonauthGuard] }
];
Run Code Online (Sandbox Code Playgroud)

NonAuthGuard 在我的情况下提供相反的行为,并且不允许用户在授权时点击登录路线.