기본 콘텐츠로 건너뛰기

angular4 custom RouteReuseStrategy

angular4 custom RouteReuseStrategy

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

from http://yamoe.tistory.com/460 by ccl(A) rewrite - 2020-03-07 13:55:35

댓글

이 블로그의 인기 게시물

[Angular] Router 라우터 정리

[Angular] Router 라우터 정리 Angular2 버전 이후를 기준으로 정리한 글입니다. 라우터는 URL을 사용하여 특정 영역에 어떤 뷰를 보여 줄지 결정하는 기능을 제공한다. 전통적인 서버사이드 렌더링을 하는 웹 사이트는 주소가 바뀔 때마다 서버에 전체 페이지를 요청하고 전체 페이지를 화면에 렌더링한다. 매 요청시 전체 페이지를 새로 랜더링하는 것은 비효율적이기 때문에 라우터를 이용하여 필요한 부분만 랜더링을 한다면 효율적일 것이다. 라우터는 URL에 해당하는 컴포넌트를 화면에 노출하고 네비게이션을 할 수 있는 기능을 가지고 있다. Router 구성 요소 Router – 라우터를 구현하는 객체이다. Navigate() 함수와 navigateByUrl() 함수를 사용하여 경로를 이동할 수 있다. RouterOulet – 라우터가 컴포넌트를 태그에 렌더링하는 영역을 구현하는 Directive이다. Routes – 특정 URL에 연결되는 컴포넌트를 지정하는 배열이다. RouterLink – HTML의 앵커 태그는 브라우저의 URL 주소를 변경하는 것이다. 앵귤러에서 RouterLink를 사용하면 라우터를 통해 렌더링할 컴포넌트를 변경할 수 있다. ActivatedRoute – 현재 동작하는 라우터 인스턴스 객체이다. Router 설정 라우터를 사용하기 위해서는 먼저 Router 모듈을 import 해야 한다. import { RouterModule, Routes } from '@angular/router'; 라우터에서 컴포넌트는 고유의 URL과 매칭된다. URL과 컴포넌트는 아래와 같이 Routes 객체를 설정하여 지정할 수 있다. 아래의 예에서는 디폴트 path에서는 MainComponent가 노출이 되고 product-list path에서는 ProductListComponent가 노출이 되도록 설정을 한 것을 볼 수 있다. const routes: Routes = [ { pa...

angular js scope와 rootscope 의 차이

angular js scope와 rootscope 의 차이 inflearn 의 Start Google Angular.js 앵귤러 과정 강좌를 보고 정리한 내용입니다. https://www.inflearn.com/course/angular-js angular js scope와 rootscope 의 차이 angularjs scope란? scope 는 controller를 적용한 태그 내에서 사용할 변수나 함수를 저장해 놓을 수 있는 객체입니다. angularjs rootscope란? rootScope 는 문서 전체에서 사용할 변수나 함수를 저장해 놓는 객체입니다. $rootScope 를 지정한 app.run() 함수는 angularjs module 객체가 생성되면 실행되는 함수입니다. 따라서 app module 객체가 생성되면 문서 전체에서 사용할 수 있는 data1, data2 변수가 생성됩니다. $rootScope 를 지정한 run 함수는 angularjs module 객체가 생성되면 실행되는 함수입니다. 따라서 app 모듈 객체가 생성되면 data1 = 100, data2 = 200이 할당됩니다. 먼저 선언된 $rootScope 보다 뒤에 선언된 controller의 data1 = 1000 에 따라 최종적으로 data1 에는 1000이 할당됩니다. controller 에서 data2에는 값을 재할당하지 않았으므로, $rootScope 에서 설정한 200이 할당되어 있습니다. controller 를 여러개 만들고, controller 마다 사용하는 data 변수를 다르게 지정해줄 수 있습니다. angularjs 관련 다른 글 ▼ angular js 예제, 강의 정리 (module, controller, service, factory) angularjs ng-model 이란? angular js scope와 rootscope 의 차이 angularjs table 예제 from http://m...