Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 根据当前可用路由列表,使第一个子路由处于活动状态_Angular_Angular Routing - Fatal编程技术网

Angular 根据当前可用路由列表,使第一个子路由处于活动状态

Angular 根据当前可用路由列表,使第一个子路由处于活动状态,angular,angular-routing,Angular,Angular Routing,假设我的Angular应用程序中有以下路由结构: children: [ { component: Access, path: 'access' }, { component: Authentication, path: 'authentication' }, { component: Domains, path: 'domains' }, {

假设我的Angular应用程序中有以下路由结构:

children: [
    {
        component: Access,
        path: 'access'
    },
    {
        component: Authentication,
        path: 'authentication'
    },
    {
        component: Domains,
        path: 'domains'
    },
    {
        component: Recovery,
        path: 'recovery'
    },
    {
        path: '**',
        pathMatch: 'full',
        redirectTo: 'authentication'
    }
]
在上面的实现中,由于我们默认使用
身份验证
路径,因此这将始终是当前选择的路径。但是,我的应用程序中有不同的用户具有不同的访问策略,有些用户无法访问
身份验证
路由。在这种情况下,他们应该显示的第一条路线是
访问
路线

是否可以选择列表中可用的第一个路径,而不是将此
身份验证
路径硬编码为默认路径

完全管理

身份验证=>访问=>域=>恢复

自定义管理员

访问=>域=>恢复

我希望
Access
成为第一个选中(活动)的,因为它是子列表中第一个可用的

可能是一个自定义解析器或保护程序,用于在运行时决定所述用户是
已满
还是
自定义
,并从中做出决定


谢谢

这听起来像是路由保护的完美用例。例如,此处检查如何实施一个:

简言之:

@Injectable()
export class AuthGuardService implements CanActivate {
  constructor(public auth: AuthService, public router: Router) {}
  canActivate(): boolean {
    if (!this.auth.isAuthenticated()) {
      this.router.navigate(['access']);
      return false;
    }
    return true;
  }
}
在你的路线上:

import { 
  AuthGuardService as AuthGuard 
} from './auth/auth-guard.service';
// ...
{
    component: Authentication,
    path: 'authentication',
    canActivate: [AuthGuard]
},