2017-10-10 50 views
0

我使用ngrx 4.0.3和angular-cli 1.1.2創建了角度爲4.4.3的項目。reducer在使用時不被調用--aot = true

一切都OK的時候我不使用AOT選項,但是當我使用--aot =真的,我可以看到,不被所謂的減速(因爲我無法看到控制檯輸出「減速機燃煤」):

行動:

import { Post } from '../models'; 
import {Action as NgRxAction} from '@ngrx/store'; 
export interface Action extends NgRxAction { 
    payload?: any; 
} 
@Injectable() 
export class PostActions { 
    static LOAD_POSTS = '[Post] Load Posts'; 
    loadPosts(): Action { 
     return { 
      type: PostActions.LOAD_POSTS 
     }; 
    } 
    ... 
    ... 
} 

效果:

import { AppState } from '../reducers'; 
import { PostActions, Action } from '../actions'; 
import { LoadHtmlServiceService } from '../services/load-html-service.service'; 
import 'rxjs/add/operator/switchMap'; 

@Injectable() 
export class PostEffects { 
    constructor(
     private update$: Actions, 
     private postActions: PostActions, 
     private svc: LoadHtmlServiceService 
    ) { } 

    @Effect() loadPosts$ = this.update$ 
     .ofType(PostActions.LOAD_POSTS) 
     .switchMap(() => this.svc.getAllSections()) 
     .map(posts => this.postActions.loadPostsSuccess(posts)); 
    ... 
    ... 
} 

減速/後列表:

import { PostActions, Action } from '../actions'; 
export type PostListState = Post[]; 
const initialState: PostListState = []; 

export default function (state = initialState, action: Action): PostListState { 
    console.log('REDUCER FIRED!!!!') 
    switch (action.type) {   
     case PostActions.LOAD_POST_SUCCESS: { 
      return action.payload; 
     } 
     ... 
     ... 
    } 
} 

減速/指數:

import postListReducer, * as fromPostList from './post-list'; 
import postReducer, * as fromPost from './post'; 
import userReducer, * as fromUser from './user'; 

export interface AppState { 
    posts: fromPostList.PostListState; 
    post: fromPost.PostState; 
    user: fromUser.UserState; 
}; 


export const reducers: ActionReducerMap<AppState> = { 
    posts: postListReducer, 
    post: postReducer, 
    user: userReducer 
}; 

app.module:

import { reducers } from './reducers'; 
import { PostEffects } from './effects'; 
@NgModule({ 
    declarations: [ 
    AppComponent 
], 
    entryComponents: [AddSectionModalComponent], 
    imports: [ 
    LayoutModule 
    StoreModule.forRoot(reducers), 
    EffectsModule.forRoot([PostEffects]) 
    ] 
... 
... 

就是什麼我忘了嗎?

感謝好NGRX peolpe

回答

1

終於想通了如何使我的代碼運行機智--aot:

訣竅是:

  1. 定義上減速injectionToken /指數

    export const reducerToken = new InjectionToken<ActionReducerMap<AppState>>('Registered Reducers');   
    Object.assign(reducerToken, reducers); 
    
  2. 限定getReducers工廠(app.module):

    export function getReducers() { 
        return reducers; 
    } 
    
  3. app.module註冊模塊時: 寄存器reducerToken和provders部提供它:

    imports:[ 
        ... 
        ... 
        StoreModule.forRoot(reducerToken), 
        EffectsModule.forRoot([PostEffects]) 
    ], 
    providers: [ 
    { 
        provide: reducerToken, 
        useFactory: getReducers 
    } 
    
相關問題