I need to know how to create a listener e.g. I want to subscribe to the AppState changing.
Below is my current very basic service. I have a dispatch action on the view which increments the counter.
Once the counter changes value I'd like to detect this in other parts of my website e.g. the global header for example.
I'm using ng2-Redux with angular version 5.
Redux service:
export interface IAppState {
counter: number;
}
export const INITIAL_APP_STATE: IAppState = {
counter: 0
};
export function appReducer(state: IAppState, action: any): IAppState {
switch (action.type) {
case 'INCREMENT':
return {
counter: state.counter + action.payload
};
}
return state;
}
I declare the actions in a file
the effects file will look like this, (effects will call the backend)
the reducer will modify the current state of your store
In my module declaration you should declare both effects and reducer actions
in the imports of my module I will declare all the reducer and effects
I hope this code will help you
angular-redux offers a very convenient way of selecting slices of your store with the
@select()
decorator.Let's say your
IAppState
would be:Then you can select the parts of your state like this:
For further information, have a look at the select docs.