Skip to content

Instantly share code, notes, and snippets.

@philipooo
Last active April 25, 2017 07:44
Show Gist options
  • Save philipooo/d4acbcf314b69df0695eb05f55c03876 to your computer and use it in GitHub Desktop.
Save philipooo/d4acbcf314b69df0695eb05f55c03876 to your computer and use it in GitHub Desktop.
import { Injectable } from "@angular/core";
import { Store } from "@ngrx/store";
import { StoreService } from "./store-service.ts"
import { myReducer, IMyState, } from "./reducer.ts"
@Injectable()
export class SomeService {
constructor(private store: Store<any>, storeService: StoreService) {
// Add the service specific reducers to the set of existing reducers.
storeService.addReducers({myReducer});
const store$ = this.store.select<IMyState>("myReducer");
// ...
}
}
import { Injectable } from "@angular/core";
import { Store, combineReducers } from "@ngrx/store";
@Injectable()
export class StoreService {
private _reducers: any = {};
constructor(private store: Store<any>) {}
addReducers(reducers: any) {
const reducerKeys = Object.keys(reducers);
for (let i = 0; i < reducerKeys.length; i++) {
const key = reducerKeys[i];
this._reducers[key] = reducers[key];
}
this.store.replaceReducer(combineReducers(this._reducers));
}
}
@nicohabets
Copy link

Works like a charm! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment