Angular Custom RouteReuseStrategy очищает маршрут от детей - PullRequest
0 голосов
/ 13 февраля 2020

После реализации пользовательского RouteReuseStrategy, позволяющего мне указать, какие маршруты не следует повторно использовать при навигации по одному и тому же маршруту (они должны перезагружать все компоненты), мои дочерние маршруты всегда будут пустыми, что нарушит работу приложения.

Я не могу понять, почему это происходит, любая помощь будет принята с благодарностью.

import { ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy } from '@angular/router';

export class CustomRouteReuseStategy implements RouteReuseStrategy {

  handlers: { [key: string]: DetachedRouteHandle } = {};

  shouldDetach(route: ActivatedRouteSnapshot): boolean {
    return this.shouldReuseOr(route.data.shouldReuse, false);
  }

  store(route: ActivatedRouteSnapshot, handle: {}): void {
    if (this.shouldReuseOr(route.data.shouldReuse, false)) {
      this.handlers[route.routeConfig.path] = handle;
    }
  }

  shouldAttach(route: ActivatedRouteSnapshot): boolean {
    return !!route.routeConfig && !!this.handlers[route.routeConfig.path];
  }

  retrieve(route: ActivatedRouteSnapshot): {} {
    if (!route.routeConfig) {
      return null;
    }
    return this.handlers[route.routeConfig.path];
  }

  shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
    return this.shouldReuseOr(curr.data.shouldReuse, false);
  }

  private shouldReuseOr(shouldReuse: boolean, or: boolean): boolean {
    return shouldReuse != null ? shouldReuse : or;
  }
}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...