DevTool
DevTool

Reputation: 319

How i export all reducer in NgRx in one index file?

Hey i have 2 reducer in Reducer folder,

commentReducer:
import { Comment } from '../models/comment.model';
import * as CommentAction from '../actions/comment.actions';
import { Action, createReducer, on } from '@ngrx/store';

export const initialState: Comment[] = [];

const commentReducer = createReducer(
  initialState,
  on(CommentAction.addcomment, (state, { fullName, comment }) => [...state, { fullName, comment }]),
  on(CommentAction.removecomment, (state, { index }) => {
    const array = [...state];
    array.splice(index, 1);
    return array;
 })
);

export function reducer(state: Comment[], action: Action) {
  return commentReducer(state, action);
}

userReducer:

import { User } from '../models/user.model';
import * as UserAction from '../actions/user.actions';
import { Action, createReducer, on } from '@ngrx/store';

export const initialState: User = {
  userId: null,
  id: null,
  title: null,
  completed: null
};

const userReducer = createReducer(
  initialState,
  on(UserAction.loaduser, (state, payload) => payload)
);

export function reducer(state: User, action: Action) {
  return userReducer(state, action);
}

I import the reducer to the app module like that: app.module:

import * as commentReducer from './reducers/comment.reducer';
import * as userReducer from './reducers/user.reducer';

    StoreModule.forRoot({
      comment: commentReducer.reducer,
      user: userReducer.reducer
    }),

But my application grow and i add more reducer. How can i make a index file in my reducer folder and export all the reducer together and make it look like this in app.module:

   StoreModule.forRoot(AllReducer),

Somthing like that... Thanks

Upvotes: 0

Views: 1083

Answers (1)

Lia
Lia

Reputation: 11982

You can put each reducer with it's actions, models or effects in a special folder in reducers folder and import them like this in app.module.ts:

import { reducers } from './reducers';

and in imports section:

StoreModule.forRoot(reducers)

see demo and structure.

Upvotes: 2

Related Questions