Стратегия повторного использования маршрута в отложенной загрузке (Angular8), нет ответа .. я использовал данные: {key: ".."} метод - PullRequest
0 голосов
/ 27 марта 2020

Есть ли какие-либо изменения, которые я должен внести в этот код? Он не отвечает ....

Требование : Путь info /: id Показать информацию о результате поиска по пути home ... Поэтому мне нужно вернуться с info /: id home без перезагрузки.

nur-routing.module.ts file

const routes: Routes = [
  { path: 'home', component: NurComponent, data:{key: 'home' }},  
  { path: 'sell', component: SelectComponent, data: {key:"",shouldDetach:"no"}},
  { path: 'pat', component: PatComponent,data:{key:"",shouldDetach:"no"} },
  {path: 'info/:id',component:InfoComponent, data:{key:'info/:id' ,shouldDetach:"no"} },
  { path: '', redirectTo:'/nurse/home' },
];'

* Эти 4 paths входит в компонент nurComponent

В качестве службы я добавил router-reuse.service.ts

import {
  RouteReuseStrategy,
  ActivatedRouteSnapshot,
  DetachedRouteHandle,
  RouterModule,
  Routes,
  UrlSegment,
} from '@angular/router';
// NurseRoutingModule
import { Injectable } from '@angular/core';



@Injectable(
  // providedIn: 'root'
)


export class RouteReuseService implements RouteReuseStrategy {

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

  shouldDetach(route: ActivatedRouteSnapshot): boolean {
    // console.log(route);
    if (route.data.shouldDetach === 'no') {
      return false;
    }

    return true;
  }

  public store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
    RouteReuseService.handlers[route.data.key] = handle;
  }

  public shouldAttach(route: ActivatedRouteSnapshot): boolean {
    return !!route.data.key && !! RouteReuseService.handlers[route.data.key];
  }

  public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
    if (!route.data.key) {
      return null;
    }
    return RouteReuseService.handlers[route.data.key];
  }

  public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
    return future.data.key === curr.data.key;
  }

} 

В nur-module.ts файл, который я добавил в nurComponent :

providers: [ 
        { provide: RouteReuseStrategy, useClass: RouteReuseService}
      ],
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...