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