메뉴(주소)를 이동해도 이전 component 내용이 유지되도록 하기 위해서
(기본은 새로 생성됨. ngOnInit, constructor 호출됨)
RouteReuseStrategy 커스트 마이징 후 app.module.ts 에 장착하면 되는데.. 적용이 쉽지 않은듯
일단, 구현한것 까지 기록함.
custom-reuse-strategy.ts
import { RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle } from "@angular/router"; /* 호출 관계 테스트 1. /proc 열기 shouldReuseRoute. future: /, curr : / retrieve : / retrieve : / retrieve : /proc/ retrieve : /proc/ shouldAttach : / shouldAttach : /proc/ 2. /mem 이동 shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /proc/, curr : /mem/ retrieve : /mem/ retrieve : /mem/ shouldDetach : /proc/ store : /proc/ shouldAttach : /mem/ 3. /net 이동 shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /mem/, curr : /net/ retrieve : /net/ retrieve : /net/ shouldDetach : /mem/ store : /mem/ shouldAttach : /net/ 4. /mem 로 다시 이동 shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /, curr : / shouldReuseRoute. future: /net/, curr : /mem/ retrieve : /mem/ retrieve : /mem/ shouldDetach : /net/ store : /net/ shouldAttach : /mem/ retrieve : /mem/ store : /mem/ */ export class CustomReuseStrategy implements RouteReuseStrategy { handlers: { [key: string]: DetachedRouteHandle } = {}; retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle { let key = this.calcKey(route); console.log('retrieve : ' + key); return this.handlers[key]; } shouldAttach(route: ActivatedRouteSnapshot): boolean { let key = this.calcKey(route); console.log('shouldAttach : ' + key); return !!this.handlers[key]; }
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void { let key = this.calcKey(route); console.log('store : ' + key); this.handlers[key] = handle; } shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean { console.log('shouldReuseRoute. future: ' + this.calcKey(future) + ', curr : ' + this.calcKey(curr)); return future.routeConfig === curr.routeConfig; } shouldDetach(route: ActivatedRouteSnapshot): boolean { console.log('shouldDetach : ' + this.calcKey(route)); return true; } calcKey(route: ActivatedRouteSnapshot) { let url = '/'; for (var i=0; i<route.pathFromRoot.length; ++i) { const r = route.pathFromRoot[i]; if (!r.url) continue; for (var j=0; j<r.url.length; ++j) { const u = r.url[j]; if (!u.path) continue; url += u.path + '/'; } } // console.log(url); if (!url.length) return undefined; return url; } } |
난 app.module.ts 하위의 layout.module.ts에 장창 함
import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { NgModule } from '@angular/core'; import { RouteReuseStrategy } from '@angular/router'; import { AppComponent } from './app.component'; import { AppRoutingModule } from './app-routing.module'; import { CustomReuseStrategy } from './common'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, BrowserAnimationsModule, AppRoutingModule ], providers: [ { provide: RouteReuseStrategy, useClass: CustomReuseStrategy } ], bootstrap: [ AppComponent ] }) export class AppModule { } |
참고:
http://closer27.github.io/frontend/2017/02/22/angular2-custom-reuse-strategy/
https://stackoverflow.com/questions/42643748/angular-2-implementing-routereusestrategy
'가지가지' 카테고리의 다른 글
CMD 특정 디렉토리에서 실행(command line windows) (0) | 2017.11.23 |
---|---|
linux 에서 utf-8 BOM 파일 생성 하기 (0) | 2017.11.17 |
angular cli 명령어 (0) | 2017.11.17 |
angular4 dynamic add remove component (0) | 2017.10.26 |
gcc 컴파일 옵션 정리 (0) | 2017.07.04 |
c++ 개발환경(vim 명령어) (0) | 2017.07.03 |
c++ 개발환경 디버깅 (gdb 명령어, strace, ltrace, splint, valgrind, ...) (0) | 2017.07.03 |
zookeeper 3.4.10 기본 설치 (0) | 2017.07.03 |