これは私がこれを達成する方法です。
最初 - 実装RouteReuseStrategyインターフェイス。第
import {RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle} from '@angular/router';
export class CustomReuseStrategy implements RouteReuseStrategy {
handlers: {[key: string]: DetachedRouteHandle} = {};
shouldDetach(route: ActivatedRouteSnapshot): boolean {
//console.debug('CustomReuseStrategy:shouldDetach', route);
return !!route.data && !!(route.data as any).shouldDetach;
}
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
//console.debug('CustomReuseStrategy:store', route, handle);
this.handlers[route.routeConfig.path] = handle;
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
//console.debug('CustomReuseStrategy:shouldAttach', route);
return !!route.routeConfig && !!this.handlers[route.routeConfig.path];
}
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
//console.debug('CustomReuseStrategy:retrieve', route);
if (!route.routeConfig) { return null; }
return this.handlers[route.routeConfig.path];
}
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
//console.debug('CustomReuseStrategy:shouldReuseRoute', future, curr);
return future.routeConfig === curr.routeConfig;
}
}
は - app.moduleにCustomReuseStrategyプロバイダを追加しました。
import {RouteReuseStrategy} from '@angular/router';
import {CustomReuseStrategy} from './Shared/reuse-strategy';
providers: [
{ provide: RouteReuseStrategy, useClass: CustomReuseStrategy }
]
サードは - あなたが離れコンポーネントから移動すると、そのコンポーネントが破壊され、そのためそのバインディングのすべてがするapp.routing.ts
import { Routes, RouterModule } from "@angular/router";
import { MapComponent } from './components/map/map.component';
const ROUTES: Routes = [
{ path: 'maps', component: MapComponent , canActivate:[AuthGuard], data: { shouldDetach: true} },
];
にshouldDetach属性を追加しました。データがある場合は、そのデータをサービスに入れてください。最初にロードされたときに再びロードされることはなく、代わりにキャッシュが使用されます。 – Lansana
@Lansanaすぐにお返事ありがとうございます。まあ私はここでキャッシュを使用することはできません。なぜなら、残りのAPIから連続したライブデータを取得して、地図上に押しピンを表示するので、単一のデータをキャッシュしたくないからです。マップコンポーネントに戻ったときにマップは元の状態にとどまるべきであるということだけです。私は地図をリフレッシュしたくない。 –
使用している地図に状態を保存するための設定オプションがありますか?そのような場合は、そのロジックをサービスに追加し、コンポーネントのinitにそのロードをロードして、マップが存在する場合はそのマップを使用できるようにします。そうでない場合は、新しいバージョンの自身を作成します。これは、コンポーネント自体のすべての状態が破棄されるため、必要なものを実現する唯一の方法です。そのため、マップは常に再作成されます。 – Lansana