feat(router): add predicate function mode for runGuardsAndResolvers (#27682)

This option means guards and resolvers will ignore changes when a provided predicate function returns `false`. This supports use cases where an application needs to ignore some param updates but not others. For example, changing a sort param in the URL might need to be ignored, whereas changing the a `project` param might require re-run of guards and resolvers.

Related to #26861 #18253 #27464

PR Close #27682
This commit is contained in:
Jason Aden
2018-12-14 14:02:50 -05:00
committed by Miško Hevery
parent 7901cd8cfb
commit 12c317603a
4 changed files with 50 additions and 2 deletions

View File

@ -8,10 +8,13 @@
import {NgModuleFactory, NgModuleRef, Type} from '@angular/core';
import {Observable} from 'rxjs';
import {EmptyOutletComponent} from './components/empty_outlet';
import {ActivatedRouteSnapshot} from './router_state';
import {PRIMARY_OUTLET} from './shared';
import {UrlSegment, UrlSegmentGroup} from './url_tree';
/**
* @description
*
@ -48,6 +51,10 @@ import {UrlSegment, UrlSegmentGroup} from './url_tree';
* - `pathParamsOrQueryParamsChange` - Same as `pathParamsChange`, but also rerun when any query
* param changes
* - `always` - Run guards and resolvers on every navigation.
* - (from: ActivatedRouteSnapshot, to: ActivatedRouteSnapshot) => boolean - Use a predicate
* function when none of the pre-configured modes fit the needs of the application. An example
* might be when you need to ignore updates to a param such as `sortDirection`, but need to
* reload guards and resolvers when changing the `searchRoot` param.
* - `children` is an array of child route definitions.
* - `loadChildren` is a reference to lazy loaded child routes. See `LoadChildren` for more
* info.
@ -369,7 +376,8 @@ export type QueryParamsHandling = 'merge' | 'preserve' | '';
* @publicApi
*/
export type RunGuardsAndResolvers = 'pathParamsChange' | 'pathParamsOrQueryParamsChange' |
'paramsChange' | 'paramsOrQueryParamsChange' | 'always';
'paramsChange' | 'paramsOrQueryParamsChange' | 'always' |
((from: ActivatedRouteSnapshot, to: ActivatedRouteSnapshot) => boolean);
/**
* See `Routes` for more details.

View File

@ -147,6 +147,9 @@ function getRouteGuards(
function shouldRunGuardsAndResolvers(
curr: ActivatedRouteSnapshot, future: ActivatedRouteSnapshot,
mode: RunGuardsAndResolvers | undefined): boolean {
if (typeof mode === 'function') {
return mode(curr, future);
}
switch (mode) {
case 'pathParamsChange':
return !equalPath(curr.url, future.url);