2017-10-10 36 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 
    } 
    
相关问题