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