angular/packages/examples/router/utils/functional_guards.ts
Andrew Scott 455c728525 feat(router): helper functions to convert class guards to functional (#48709)
This commit introduces helper functions to easily convert `Injectable`s with
functions compatible with `Route` guards to the corresponding guard functions.
These functions will serve to aid in migrating off of the now deprecated
class-based guards, but also provide an easy avenue to still defining
guards as `Injectable` classes if that is desired.

PR Close #48709
2023-02-27 11:52:47 -08:00

40 lines
801 B
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Injectable} from '@angular/core';
import {mapToCanActivate, mapToResolve, Route} from '@angular/router';
// #docregion CanActivate
@Injectable({providedIn: 'root'})
export class AdminGuard {
canActivate() {
return true;
}
}
const route: Route = {
path: 'admin',
canActivate: mapToCanActivate([AdminGuard]),
};
// #enddocregion
// #docregion Resolve
@Injectable({providedIn: 'root'})
export class ResolveUser {
resolve() {
return {name: 'Bob'};
}
}
const userRoute: Route = {
path: 'user',
resolve: {
user: mapToResolve(ResolveUser),
},
};
// #enddocregion