lundi 29 mars 2021

Clear RouteReuseStrategy on certain page

I implement RouteReuseStrategy on my project, then I specifically stated certain component can be reuse. Now my question is when I am in certain page, I wish to clear all the snapshot and reset all the reuse component. What is the best approach I can do?

Sample flow of my page

page A --> page B (cache for reuse) --> page C

page C --> page A (I wish to clear cache here)

I know we can implement a function to clear handlers in this CustomReuse class but whenever I call the function on other component I will hit NullInjectorError: StaticInjectorError(AppModule)

Below are my RouteReuseStrategy code

export class CustomRouteReuseStrategy implements RouteReuseStrategy {
  
  private handlers = new Map<string, DetachedRouteHandle>();
  
  constructor() {

  }

  shouldDetach(route: ActivatedRouteSnapshot): boolean {
    return true;
  }

  store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
    this.handlers[route.url.join("/") || route.parent.url.join("/")] = handle;
  }

  shouldAttach(route: ActivatedRouteSnapshot): boolean {
    return !!this.handlers[route.url.join("/") || route.parent.url.join("/")];
  }

  retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
    return this.handlers[route.url.join("/") || route.parent.url.join("/")];
  }

  shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
    return future.routeConfig === curr.routeConfig;
  }



Aucun commentaire:

Enregistrer un commentaire