博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular] How to get Store state in ngrx Effect
阅读量:4577 次
发布时间:2019-06-08

本文共 2918 字,大约阅读时间需要 9 分钟。

For example, what you want to do is navgiate from current item to next or previous item. 

In your component, you can dispatch action like this:

next($event) {    $event.preventDefault();    this.store.dispatch(new skillAction.Next(this.key));  }

 

So here is the action defination:

export const NEXT = '[Skill] Next';export class Next implements Action {  readonly type = NEXT;  constructor(public payload: string) {}}

As you can see, the payload is current key / id for the current item.

 

Now in the effect class, we can get current item's key from payload, we still need to know what is the next item's id in the collection.

Luckly we have selector function, which looks like this:

export const getNextSkill = createSelector(  getCollectionSkillIds,  getSelectedSkillId,  (ids, selectedId) => getNext(selectedId, ids));

 

Ok, now, in the effect, we should be able to get all what we need:

import {Injectable} from '@angular/core';import {Actions, Effect} from '@ngrx/effects';import {Router} from '@angular/router';import * as actions from '../actions/skill';import * as fromSkill from '../reducers';import 'rxjs/add/operator/do';import 'rxjs/add/operator/map';import 'rxjs/add/operator/switchMap';import 'rxjs/add/operator/withLatestFrom';import 'rxjs/add/operator/distinctUntilChanged';import {Observable} from 'rxjs/Observable';import {Action, Store} from '@ngrx/store';import {SkillsService} from '../services/skills.service';import {Skill} from '../models/skills';import {of} from 'rxjs/observable/of';import {fromPromise} from 'rxjs/observable/fromPromise';import {MatSnackBar} from '@angular/material';@Injectable()export class SkillEffects {  constructor(private skillsService: SkillsService,              private actions$: Actions,              private store: Store
, private router: Router, private snackBar: MatSnackBar) { } @Effect({dispatch: false}) selectedSkill$: Observable
= this.actions$ .ofType(actions.SELECT) .do((action: actions.Select) => this.router.navigateByUrl(`/dashboard/(skills/${action.payload}//aside:skills)`)); @Effect() nextSkill$: Observable
= this.actions$ .ofType(actions.NEXT) .withLatestFrom(this.store.select(fromSkill.getNextSkill)) .map(([action, next]) => new actions.Select(next));}

The most important piece here is 'withLatestFrom', we can select our selector which return an Observable. Now we are able to acess the state and get just what we want from the state.

 

Notice here, in the end we map to a new action which is "SELECT" action, we want it to sync our URL bar with UI state. This is important for the applcation, we need to make sure that Router (URL) should be our single souce of turth, only when URL changed, then we can change our UI State, otherwise, there might be chacne, URL and UI are out of sync.

转载于:https://www.cnblogs.com/Answer1215/p/7776345.html

你可能感兴趣的文章
hello,world !
查看>>
【Entity Framework】Model First Approach
查看>>
C# DataTable删除行Delete与Remove的问题
查看>>
HDU2586How far away? LCA
查看>>
网络流 - 最大流
查看>>
随手记note(记事簿)
查看>>
JRE System Library 与Java EE Libraries的区别
查看>>
sqlite3性能优化要点
查看>>
颜色分类函数
查看>>
Oracle数据泵详解
查看>>
(中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
查看>>
sort-归并排序
查看>>
django 快速实现完整登录系统(cookie)
查看>>
.NET中的out和ref关键字
查看>>
Python之ftp服务器
查看>>
KMP预处理
查看>>
oracle的wm_concat函数实现行转列
查看>>
C语 三子棋小游戏
查看>>
[BZOJ 1861] 书架
查看>>
Unity NGUI 批量点击跳转场景
查看>>