Last active
April 25, 2017 07:44
-
-
Save philipooo/d4acbcf314b69df0695eb05f55c03876 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
// ... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works like a charm! Thank you.