CanLoad
인터페이스
사용 중단 권고
자식 라우팅 규칙을 로드할 수 있는지 판단하는 가드가 되도록 클래스에서 구현할 수 있는 인터페이스입니다.
모든 가드가 true를 반환하면 탐색을 계속합니다.
하나라도 false를 반환하면 탐색을 취소합니다.
가드가 UrlTree를 반환하면 현재 탐색이 취소되고 가드가 반환한 UrlTree로 새 탐색을 시작합니다.
사용 중단을 권고합니다.
설명
자식 라우팅 규칙을 로드할 수 있는지 판단하는 가드가 되도록 클래스에서 구현할 수 있는 인터페이스입니다.
모든 가드가 true를 반환하면 탐색을 계속합니다.
하나라도 false를 반환하면 탐색을 취소합니다.
가드가 UrlTree를 반환하면 현재 탐색이 취소되고 가드가 반환한 UrlTree로 새 탐색을 시작합니다.
다음 예시는 현재 사용자가 요청한 자식 라우팅 규칙을 로드할 권한이 있는지 판단하는 CanLoad 함수를 구현합니다.
class UserToken {}
class Permissions {
canLoadChildren(user: UserToken, id: string, segments: UrlSegment[]): boolean {
return true;
}
}
@Injectable()
class CanLoadTeamSection implements CanLoad {
private readonly permissions = inject(Permissions);
private readonly currentUser = inject(UserToken);
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean>|Promise<boolean>|boolean {
return this.permissions.canLoadChildren(this.currentUser, route, segments);
}
}
여기서는 라우터 구성의 Route 객체에 정의한 가드 함수를 지정합니다:
@NgModule({
imports: [
RouterModule.forRoot([
{
path: 'team/:id',
component: TeamComponent,
loadChildren: () => import('./team').then(mod => mod.TeamModule),
canLoad: [CanLoadTeamSection]
}
])
],
providers: [CanLoadTeamSection, UserToken, Permissions]
})
class AppModule {}
상세 내용으로 이동