Skip to content

Commit ca79880

Browse files
abarghoudatscott
authored andcommitted
fix(router): export DefaultRouteReuseStrategy to Router public_api (angular#31575)
export DefaultRouteStrategy class that was used internally and exposed, and add documentation for each one of methods PR Close angular#31575
1 parent b071495 commit ca79880

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

goldens/public-api/router/router.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ export declare class ActivationStart {
5151
toString(): string;
5252
}
5353

54+
export declare abstract class BaseRouteReuseStrategy implements RouteReuseStrategy {
55+
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle | null;
56+
shouldAttach(route: ActivatedRouteSnapshot): boolean;
57+
shouldDetach(route: ActivatedRouteSnapshot): boolean;
58+
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean;
59+
store(route: ActivatedRouteSnapshot, detachedTree: DetachedRouteHandle): void;
60+
}
61+
5462
export declare interface CanActivate {
5563
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree;
5664
}

packages/router/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export {RouterLinkActive} from './directives/router_link_active';
1313
export {RouterOutlet} from './directives/router_outlet';
1414
export {ActivationEnd, ActivationStart, ChildActivationEnd, ChildActivationStart, Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouterEvent, RoutesRecognized, Scroll} from './events';
1515
export {CanActivate, CanActivateChild, CanDeactivate, CanLoad, Resolve} from './interfaces';
16-
export {DetachedRouteHandle, RouteReuseStrategy} from './route_reuse_strategy';
16+
export {BaseRouteReuseStrategy, DetachedRouteHandle, RouteReuseStrategy} from './route_reuse_strategy';
1717
export {Navigation, NavigationExtras, Router} from './router';
1818
export {ROUTES} from './router_config_loader';
1919
export {ExtraOptions, InitialNavigation, provideRoutes, ROUTER_CONFIGURATION, ROUTER_INITIALIZER, RouterModule} from './router_module';

packages/router/src/route_reuse_strategy.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,54 @@ export abstract class RouteReuseStrategy {
6060
}
6161

6262
/**
63-
* Does not detach any subtrees. Reuses routes as long as their route config is the same.
63+
* @description
64+
*
65+
* This base route reuse strategy only reuses routes when the matched router configs are
66+
* identical. This prevents components from being destroyed and recreated
67+
* when just the fragment or query parameters change
68+
* (that is, the existing component is _reused_).
69+
*
70+
* This strategy does not store any routes for later reuse.
71+
*
72+
* Angular uses this strategy by default.
73+
*
74+
*
75+
* It can be used as a base class for custom route reuse strategies, i.e. you can create your own
76+
* class that extends the `BaseRouteReuseStrategy` one.
77+
* @publicApi
6478
*/
65-
export class DefaultRouteReuseStrategy implements RouteReuseStrategy {
79+
export abstract class BaseRouteReuseStrategy implements RouteReuseStrategy {
80+
/**
81+
* Whether the given route should detach for later reuse.
82+
* Always returns false for `BaseRouteReuseStrategy`.
83+
* */
6684
shouldDetach(route: ActivatedRouteSnapshot): boolean {
6785
return false;
6886
}
87+
88+
/**
89+
* A no-op; the route is never stored since this strategy never detaches routes for later re-use.
90+
*/
6991
store(route: ActivatedRouteSnapshot, detachedTree: DetachedRouteHandle): void {}
92+
93+
/** Returns `false`, meaning the route (and its subtree) is never reattached */
7094
shouldAttach(route: ActivatedRouteSnapshot): boolean {
7195
return false;
7296
}
97+
98+
/** Returns `null` because this strategy does not store routes for later re-use. */
7399
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle|null {
74100
return null;
75101
}
102+
103+
/**
104+
* Determines if a route should be reused.
105+
* This strategy returns `true` when the future route config and current route config are
106+
* identical.
107+
*/
76108
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
77109
return future.routeConfig === curr.routeConfig;
78110
}
79111
}
112+
113+
export class DefaultRouteReuseStrategy extends BaseRouteReuseStrategy {}

0 commit comments

Comments
 (0)