ngrx

How to get access of the state tree in effects? (@ngrx/effects 2.x)

假如想象 提交于 2019-11-27 14:31:56
问题 I am updating @ngrx/effects from 1.x to 2.x In 1.x I have access of state tree in effect: constructor(private updates$: StateUpdates<AppState>) {} @Effect() bar$ = this.updates$ .whenAction(Actions.FOO) .map(obj => obj.state.user.isCool) .distinctUntilChanged() .filter(x => x) .map(() => ({ type: Actions.BAR })); Now in 2.x, it only gives me action. Is there still a way to get access of the state tree? Or should I avoid using like this because it is not a good practice? constructor(private

What are benefits of using store (ngrx) in angular 2

纵然是瞬间 提交于 2019-11-27 09:40:17
问题 I'm working on a angular 1.x.x project and thinking about upgrading my code to angular 2 . Now in my project I have many services(factory) for handling data which almost keep data in js arrays(both cache and storage) and process these data by using underscore for handling arrays. I found that many examples in angular2 using ngrx. What are benefits of using store compare to use data services to handle data? Do I need multiple store for my app if I have multiple data type (stock, order,

ngrx effects error handling

ⅰ亾dé卋堺 提交于 2019-11-27 08:37:35
I have a very basic question concerning @ngrx effects: How to ignore an error that happens during the execution of an effect such that it doesn't affect future effect execution? My situation is as follows: I have an action (LOGIN) and an effect listening to that action. If an error happens inside this effect, I want to ignore it. When LOGIN is dispatched a second time after this error, the effect should be executed a second time. My first attempt to do this was: @Effect() login$ = this.actions$ .ofType('LOGIN') .flatMap(async () => { console.debug('LOGIN'); // throw an error let x = [];x[0]();

tap() isn't triggered in RXJS Pipe

只谈情不闲聊 提交于 2019-11-27 07:47:14
问题 I have to ways of doing the same thing, although I prefer the first one. But the first approach doesn't seem to work. (the tap() is not triggered) // does not work this.actions$.pipe( ofType(LayoutActions.Types.CHANGE_THEME), takeUntil(this.destroyed$), tap(() => { console.log('test') }), ); // works this.actions$.ofType(LayoutActions.Types.CHANGE_THEME).subscribe(() => { console.log('test') }); 回答1: Imagine RxJS pipes like actual, physical pipes with a valve at the end. Each pipe will

@ngrx Effect does not run the second time

我的梦境 提交于 2019-11-27 06:43:00
问题 I've just started learning about @ngrx/store and @ngrx.effects and have created my first effect in my Angular/Ionic app. It runs ok the first time but if I dispatch the event to the store again (i.e when clicking the button again), nothing happens (no network call is made, nothing in console logs). Is there something obvious I'm doing wrong? Here's the effect: @Effect() event_response$ = this.action$ .ofType(SEND_EVENT_RESPONSE_ACTION) .map(toPayload) .switchMap((payload) => this.myService

Ngrx Store Resets after browser refresh. How to make the application preserve the state?

六眼飞鱼酱① 提交于 2019-11-27 06:02:37
问题 I Dispatch a action from one component this.store.dispatch({type : STORE_TEAMCREST , payload : team.crestURI}); and in the other component i select from the store using this.store.select(state => state.table.teamCrest).subscribe(data => this.teamCrest = data); This works fine if my app is going sequentially backward or forward, but once i do a browser refresh the state loses it's value. How to preserve it's value so that it works on browser refresh? 回答1: Your store has a subscribe function

Rx.Subject loses events

北战南征 提交于 2019-11-27 04:51:24
问题 Can anybody explain what the differents between these 3 variants? http://jsfiddle.net/8vx2g3fr/2/ First works as excpect, all events are processed. But second loses last event (3) Third loses second event (2) Could you please help me to understand what the issue is and how to make the third variant process all events? 1 let bs = new Rx.Subject(); bs .subscribe(v=>{ console.log("in", v); if (v % 2 == 0) { setTimeout(()=>{ console.log(" out", v, "->" , v + 1); bs.next(v+1); }, 0); } }); bs.next

how to subscribe to action success callback using ngrx and effects

怎甘沉沦 提交于 2019-11-27 00:23:27
问题 I'm trying to do simple thing - after some entity saved (using http request) I want to navigate back to list route. The problem is : how to subscribe to success action (or may be reducer or effect?) here is my actions code: static SAVE_POST = '[POST] Save POST'; savePOST(Post): Action { return { type: PostActions.SAVE_POST, payload: Post }; } static SAVE_POST_SUCCESS = '[POST] Save POST Success'; savePOSTSuccess(Post): Action { console.log('action: savePostSuccess') return { type: PostActions

Angular 2 update global state as a side effect of updating a certain state

可紊 提交于 2019-11-26 23:37:04
问题 I want to achieve setting a global state while also requesting data from an api and storing that in the state as well, but in another location than the global state. I'm calling this effect ( models.effects.ts ): @Effect() models$: Observable<Action> = this.actions$ .ofType(GET_MODELS) .switchMap(() => this.modelsApi.getModels()) .map(models => ({type: SET_MODELS, payload: models})) .catch((err: any) => Observable.of({type: GET_FAILURE, payload: {error: err}})) Now what I want to do is

SwitchMap vs MergeMap in the #ngrx example

情到浓时终转凉″ 提交于 2019-11-26 22:36:11
问题 Below is code from the Ngrx example: https://github.com/ngrx/example-app/blob/master/src/effects/book.ts My question is why in the first @Effect, it uses switchMap while the others use mergeMap . Is that because the first @Effect is dealing with network, and with the switchMap you can cancel the previous network request if it's running? @Effect() search$ = this.updates$ .whenAction(BookActions.SEARCH) .map<string>(toPayload) .filter(query => query !== '') .switchMap(query => this.googleBooks