typescript - Angular 2 effect is not working -
reducer:
import {actionreducer, action} '@ngrx/store'; import {set_brands, set_brand} './brands.actions'; import {ibrandsstorage} './brands-storage.interface'; export const brandsreducer: actionreducer<any> = (state: ibrandsstorage = {list: [], single: {}}, action: action) => { switch(action.type) { case set_brands: return object.assign({}, state, { list: [...action.payload.data] }); case set_brand: return object.assign({}, state, { single: action.payload.data }); } }
effect:
import {injectable} '@angular/core'; import {action, store} '@ngrx/store'; import {actions, effect} '@ngrx/effects'; import {observable} 'rxjs/observable'; import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/switchmap'; import {get_brands, get_brand} './brands.actions'; import {brandsapi} 'app/shared/apis'; @injectable() export class brandseffects { constructor(private brandsapi: brandsapi, private store: store, private actions$: actions) {} @effect() brands$: observable<action> = this.actions$ .oftype(get_brands) .switchmap(() => this.brandsapi.getbrands()) .map(brands => this.store.dispatch({type: set_brands, payload: brands})) // todo: add catch @effect() brand$: observable<action> = this.actions$ .oftype(get_brand) .switchmap(() => this.brandsapi.getbrand()) .map(brand => this.store.dispatch({type: set_brand, payload: brand})) // todo: add catch }
and in component in ngoninit
call this:
this.store.dispatch({type: get_brands});
and error thrown uncaught (in promise): typeerror: unknown type returned
, i'm guessing because haven't defined in reducer. don't want define get_brands
in reducer won't state, want have set
methods in reducer set data api.
i'm not sure if correct way of doing though, can shed light on this?
edit:
i tried add get_brands
action reducer set isbusy
state true
gives me same error i'm not sure issue..
i looked @ this question suggests switching switchmapto
switchmap
i'm using switchmap
..
use with:
.map(brands => ({type: set_brands, payload: brands}))
you don't need use store.dispatch.
did imported set_brands actions in brandseffects? seams not.
Comments
Post a Comment