2017-11-10 8 views
1

example application provided by NgRx에 대한 코드를 검토하고있었습니다. 예제 응용 프로그램의 각 감속기 함수가 해당 감속기에 대한 State 인터페이스에 의해 입력 된 반환 값을 가지고있는 것으로 나타났습니다. (나는 오렌 Farhi에 의해 Reactive Programming with Angular and NgRx 제목 NgRx에 대한 책을 읽고 있었다, 나중에상태 vs ActionReducer <State> as NgRx reducer 반환 유형

export interface State { 
    ids: string[]; 
    entities: { [id: string]: Book }; 
    selectedBookId: string | null; 
} 

export const initialState: State = { 
    ids: [], 
    entities: {}, 
    selectedBookId: null, 
}; 

export function reducer(
    state = initialState, 
    action: book.Actions | collection.Actions 
): State { 

및 감속기 함수의 일반적인 신체 구조를 보여주는 코드 건너 온 : 예를 들어, 책 감속기는 다음과 같은 코드가 있습니다 .

export interface SomeInterface { 
    items: Item[], 
    filter: string 
} 

let initialState: SomeInterface = { 
    items: [], 
    filter: '' 
} 

export function MyReducer (
    state: SomeInterface = initialState, 
    action: Action 
): ActionReducer<SomeInterface> { 

: 24-25 페이지에) 일반적인 구조에 대한 코드는이 경우 SomeInterface보다는 State 불리는 타입 파라미터()로 State으로 ActionReducer가 입력되는 것으로 감속기 함수의 리턴 값을 나타낸다 왜 하나의 코드 샘플은 State와 다른 코드 샘플을 사용합니까? 감속기 함수의 반환 값에 대한 유형 매개 변수로 State가있는 ActionReducer를 사용합니까? 왜 다른 사람보다이 기능 서명 중 하나를 선택하겠습니까? 각각의 목적은 무엇입니까?

이 책은 NgRx 2.2.1 용으로 작성되었지만 예제 응용 프로그램은 최신 버전의 NgRx (버전 4.1.1) 용입니다. 반환 유형의 차이는 NgRx의 차이로 간단히 설명 할 수 없다고 생각합니다. NgRx의 최신 버전에는 ActionReducer도 있습니다.

감사합니다.

+0

사용 정의이 [* * medium blog **] (https://medium.com/@aravindfz/setting-up-storemodule-in-ngrx-4-0-b7c60732aa64) ngrx-4.0을 설정하십시오. – Aravind

답변

2

ActionReducer 수입시 StoreModule에 전달되는 감속기의 모음입니다

  • 감속기는 항상 (귀하의 경우 SomeInterface) 초기 상태의 유형을 반환해야

    export interface SomeInterface{ 
        .... 
    } 
    const initialState: SomeInterface= { 
        .... 
    }; 
    export function reducer(state = initialState, action: Actions): SomeInterface{...} 
    
  • ActionReducer는 감속기 모음이되어야합니다. 유형 인터페이스가 필요합니다. 프로파일 저장소 애플리케이션의 인터페이스 및이를 수집 감속기 공장

    export const reducers: ActionReducerMap<AppStore> = { 
         someInterfacerSlice: someInterface.reducer, 
    }; 
    
  • 호출되는 바와 같이 아래 모듈 용 글로벌 앱 스토어 인터페이스

    export interface AppStore { 
         someInterfaceSlice: SomeInterface; 
         stateSlice: StateSlice; 
    } 
    
+0

그래서이 책은 "Angular와 NgRx를 이용한 리 액티브 프로그래밍"은 코드에 오류가 있으며, 감속기의 반환 유형으로 ActionReducer 대신 SomeInterface가 있습니까? – user2245766

+0

내가 이해하지 못했다 – Aravind

+0

@ user2245766 upvote 너무 :) – Aravind