2016-10-04 5 views
4

우리는 프로젝트에서 각도 2를 사용하고 있습니다. 지금까지 우리는 개발에 우리의 데이터 서비스에 in-memory-web-api를 사용각도 2 : 다중 HTTP 서비스

app.module.ts :

imports: [ 
    HttpModule, 
    InMemoryWebApiModule.forRoot(MockData), 
    ... 
] 

data.service.ts :

constructor(private http: Http) { } 

지금이 시간에이다 실제 데이터를 가져옵니다. 그러나 모의 데이터를 한꺼번에 대체 할 수는 없습니다. 그래서 우리는 점차적으로 프로젝트가 진행에서 모의 ​​데이터를 이동할 수

constructor(private fakeHttp: FakeHttp, /* this one use in-memory-api */ 
      private http: Http /* this one goes to real remote server */) { } 

: 어떻게 같은 내 데이터 서비스를 구성해야합니까?

답변

7

이 "못생긴"2 개의 HTTPs "를 시도하는 대신 angular-in-memory-web-api은 몇 가지 옵션을 제공합니다.

0.1.3부터 ​​시작하여 모든 발견되지 않은 콜렉션 호출을 일반 XHR로 전달하는 구성 등록 정보가 있습니다.

InMemoryWebApiModule.forRoot(MockData, { 
    passThruUnknownUrl: true 
}) 

이렇게하면 컬렉션을 찾을 수없는 요청이 실제 XHR로 전달됩니다. 그래서 한 가지 옵션은 requried로서 메모리 내 DB에서 콜렉션을 점진적으로 제거하는 것입니다.

class MockData implements InMemoryDbService { 
    createDb() { 
    let cats = []; 
    let dogs = []; 
    return { 
     cats, 
     // dogs 
    }; 
    } 
} 

가되면, DB에서 dogs 모음을 제거 메모리 것입니다 이제 실제 백엔드에 앞으로 모든 개 요청.

이것은 단지 컬렉션 수준의 수정입니다. 그러나 더욱 세분화 된 제어가 필요하면 메소드 인터셉터를 사용할 수 있습니다.

MockData 클래스에서 get 메서드를 재정의하려면 MockData 클래스에 HttpMethodInterceptorArgs 인수와 함께 추가하면됩니다.

class MockData implements InMemoryDbService { 
    get(args: HttpMethodInterceptorArgs) { 
    // do what you will here 
    } 
} 
예를 들어

HttpMethodInterceptorArgs: { 
    requestInfo: { 
    req: Request (original request object) 
    base 
    collection 
    collectionName 
    headers 
    id 
    query 
    resourceUrl 
    } 
    passThruBackend: { 
    The Original XHRBackend (in most cases) 
    } 
    config: { 
    this is the configuration object you pass to the module.forRoot 
    } 
    db: { 
    this is the object from creatDb 
    } 
} 

을 (당신은 당신이 그것으로 무엇을 할 수 있는지의 아이디어가 바로 때문에) 다음과 같이 HttpMethodInterceptorArgs의 구조는

, 여기 당신의 경우처럼 보일 것 인 것이다 방금 모든 요청을 전달했습니다.

get(args: HttpMethodInterceptorArgs) { 
    return args.passthroughBackend.createConnection(args.requstInfo.req).response 
} 
+0

쿨! 정확히 내가 찾고 있어요. 고맙습니다!! – okeydoky

관련 문제