feat(router): guards and data resolvers can now return promises
This commit is contained in:
parent
9e3d13f61f
commit
a5dc5705a3
@ -242,7 +242,8 @@ export type RouterConfig = Route[];
|
|||||||
* - `outlet` is the name of the outlet the component should be placed into.
|
* - `outlet` is the name of the outlet the component should be placed into.
|
||||||
* - `canActivate` is an array of DI tokens used to look up CanActivate handlers. See {@link
|
* - `canActivate` is an array of DI tokens used to look up CanActivate handlers. See {@link
|
||||||
* CanActivate} for more info.
|
* CanActivate} for more info.
|
||||||
* - `canActivateChild` is an array of DI tokens used to look up CanActivateChild handlers. See {@link
|
* - `canActivateChild` is an array of DI tokens used to look up CanActivateChild handlers. See
|
||||||
|
* {@link
|
||||||
* CanActivateChild} for more info.
|
* CanActivateChild} for more info.
|
||||||
* - `canDeactivate` is an array of DI tokens used to look up CanDeactivate handlers. See {@link
|
* - `canDeactivate` is an array of DI tokens used to look up CanDeactivate handlers. See {@link
|
||||||
* CanDeactivate} for more info.
|
* CanDeactivate} for more info.
|
||||||
|
@ -66,7 +66,8 @@ export interface CanActivate {
|
|||||||
* class CanActivateTeam implements CanActivate {
|
* class CanActivateTeam implements CanActivate {
|
||||||
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
|
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
|
||||||
*
|
*
|
||||||
* canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean> {
|
* canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean>
|
||||||
|
* {
|
||||||
* return this.permissions.canActivate(this.currentUser, this.route.params.id);
|
* return this.permissions.canActivate(this.currentUser, this.route.params.id);
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
|
@ -12,6 +12,7 @@ import 'rxjs/add/operator/mergeAll';
|
|||||||
import 'rxjs/add/operator/reduce';
|
import 'rxjs/add/operator/reduce';
|
||||||
import 'rxjs/add/operator/every';
|
import 'rxjs/add/operator/every';
|
||||||
import 'rxjs/add/observable/from';
|
import 'rxjs/add/observable/from';
|
||||||
|
import 'rxjs/add/observable/fromPromise';
|
||||||
import 'rxjs/add/observable/forkJoin';
|
import 'rxjs/add/observable/forkJoin';
|
||||||
import 'rxjs/add/observable/of';
|
import 'rxjs/add/observable/of';
|
||||||
|
|
||||||
@ -596,6 +597,8 @@ class PreActivation {
|
|||||||
function wrapIntoObservable<T>(value: T | Observable<T>): Observable<T> {
|
function wrapIntoObservable<T>(value: T | Observable<T>): Observable<T> {
|
||||||
if (value instanceof Observable) {
|
if (value instanceof Observable) {
|
||||||
return value;
|
return value;
|
||||||
|
} else if (value instanceof Promise) {
|
||||||
|
return Observable.fromPromise(value);
|
||||||
} else {
|
} else {
|
||||||
return Observable.of(value);
|
return Observable.of(value);
|
||||||
}
|
}
|
||||||
|
@ -891,6 +891,40 @@ describe('Integration', () => {
|
|||||||
expect(location.path()).toEqual('/');
|
expect(location.path()).toEqual('/');
|
||||||
})));
|
})));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('should work when returns a promise', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
addProviders([{
|
||||||
|
provide: 'CanActivate',
|
||||||
|
useValue: (a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
||||||
|
if (a.params['id'] == '22') {
|
||||||
|
return Promise.resolve(true);
|
||||||
|
} else {
|
||||||
|
return Promise.resolve(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}]);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
fit('works',
|
||||||
|
fakeAsync(inject(
|
||||||
|
[Router, TestComponentBuilder, Location],
|
||||||
|
(router: Router, tcb: TestComponentBuilder, location: Location) => {
|
||||||
|
const fixture = createRoot(tcb, router, RootCmp);
|
||||||
|
|
||||||
|
router.resetConfig(
|
||||||
|
[{path: 'team/:id', component: TeamCmp, canActivate: ['CanActivate']}]);
|
||||||
|
|
||||||
|
router.navigateByUrl('/team/22');
|
||||||
|
advance(fixture);
|
||||||
|
expect(location.path()).toEqual('/team/22');
|
||||||
|
|
||||||
|
router.navigateByUrl('/team/33');
|
||||||
|
advance(fixture);
|
||||||
|
expect(location.path()).toEqual('/team/22');
|
||||||
|
})));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('CanDeactivate', () => {
|
describe('CanDeactivate', () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user