Browse Source

refactor: move reloadRoute function to route-utils.ts

pull/5333/head
mehmet-erim 6 years ago
parent
commit
8edcac597a
  1. 17
      npm/ng-packs/packages/core/src/lib/services/replaceable-components.service.ts
  2. 17
      npm/ng-packs/packages/core/src/lib/utils/route-utils.ts

17
npm/ng-packs/packages/core/src/lib/services/replaceable-components.service.ts

@ -5,6 +5,7 @@ import { BehaviorSubject, Observable } from 'rxjs';
import { noop } from '../utils/common-utils';
import { map, filter } from 'rxjs/operators';
import { InternalStore } from '../utils/internal-store-utils';
import { reloadRoute } from '../utils/route-utils';
@Injectable({ providedIn: 'root' })
export class ReplaceableComponentsService {
@ -27,20 +28,6 @@ export class ReplaceableComponentsService {
}
// TODO: Create a shared service for route reload and more
private reloadRoute() {
const { shouldReuseRoute } = this.router.routeReuseStrategy;
const setRouteReuse = (reuse: typeof shouldReuseRoute) => {
this.router.routeReuseStrategy.shouldReuseRoute = reuse;
};
setRouteReuse(() => false);
this.router.navigated = false;
this.ngZone.run(async () => {
await this.router.navigateByUrl(this.router.url).catch(noop);
setRouteReuse(shouldReuseRoute);
});
}
add(replaceableComponent: ReplaceableComponents.ReplaceableComponent, reload?: boolean): void {
const replaceableComponents = [...this.store.state];
@ -57,7 +44,7 @@ export class ReplaceableComponentsService {
this.store.patch(replaceableComponents);
if (reload) this.reloadRoute();
if (reload) reloadRoute(this.router, this.ngZone);
}
get(replaceableComponentKey: string): ReplaceableComponents.ReplaceableComponent {

17
npm/ng-packs/packages/core/src/lib/utils/route-utils.ts

@ -2,6 +2,8 @@ import { PRIMARY_OUTLET, Router, UrlSegmentGroup } from '@angular/router';
import { ABP } from '../models/common';
import { RoutesService } from '../services/routes.service';
import { TreeNode } from './tree-utils';
import { noop } from './common-utils';
import { NgZone } from '@angular/core';
export function findRoute(routes: RoutesService, path: string): TreeNode<ABP.Route> {
const node = routes.find(route => route.path === path);
@ -23,3 +25,18 @@ export function getRoutePath(router: Router, url = router.url) {
return '/' + (primaryGroup || emptyGroup).segments.map(({ path }) => path).join('/');
}
export function reloadRoute(router: Router, ngZone: NgZone) {
const { shouldReuseRoute } = router.routeReuseStrategy;
const setRouteReuse = (reuse: typeof shouldReuseRoute) => {
router.routeReuseStrategy.shouldReuseRoute = reuse;
};
setRouteReuse(() => false);
router.navigated = false;
ngZone.run(async () => {
await router.navigateByUrl(router.url).catch(noop);
setRouteReuse(shouldReuseRoute);
});
}

Loading…
Cancel
Save