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<fromSkill.State>,
private router: Router,
private snackBar: MatSnackBar) {
} @Effect({dispatch: false})
selectedSkill$: Observable<Action> = this.actions$
.ofType(actions.SELECT)
.do((action: actions.Select) =>
this.router.navigateByUrl(`/dashboard/(skills/${action.payload}//aside:skills)`)); @Effect()
nextSkill$: Observable<Action> = 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.

[Angular] How to get Store state in ngrx Effect的更多相关文章

  1. [Angular] NgRx/effect, why to use it?

    See the current implementaion of code, we have a smart component, and inside the smart component we ...

  2. 用computed返回this.$store.state.count,store更改了,但是computed没有调用

    今天出现了这个问题,store更新了,你computed为啥不调用呢??? 另一个.vue更新了state,这个的computed就监听不到了么? 是用这种格式更新的this.$store.commi ...

  3. vuex this.$store.state.属性和mapState的属性中的一点点区别

    做泰康公众号的项目时候有一个需求创建公众号的时候后台有一个社区id提供给后台展现人员和部门,在群发消息时候也要给后台一个社区id只不过获取社区的id接口和上一个不是一样的,本来在页面中写了两个sele ...

  4. Do not mutate vuex store state outside mutation handlers.

    组件代码: selectItem(item,index) { this.selectPlay({ list: this.songs, index }) }, ...mapActions([ 'sele ...

  5. 【vue store的使用方法】(this.$store.state this.$store.getters this.$store.dispatch this.$store.commit)

    vue 页面文件 <template> <div> {{this.$store.state.count}}<br/> {{count}}<br/> {{ ...

  6. [Angular] Creating an Observable Store with Rx

    The API for the store is really simple: /* set(name: string, state: any); select<T>(name: stri ...

  7. [Angular2 Animation] Control Undefined Angular 2 States with void State

    Each trigger starts with an “undefined” state or a “void” state which doesn’t match any of your curr ...

  8. Session not active, could not store state 的解决方法

    1.开口加上session_start() http://metah.ch/blog/2014/05/facebook-sdk-4-0-0-for-php-a-working-sample-to-ge ...

  9. VUEX报错 [vuex] Do not mutate vuex store state outside mutation handlers

    数组 错误的写法:let listData= state.playList; // 数组深拷贝,VUEX就报错 正确的写法:let listDate= state.playList.slice(); ...

随机推荐

  1. POJ-2142 The Balance 扩展欧几里德(+绝对值和最小化)

    题目链接:https://cn.vjudge.net/problem/POJ-2142 题意 自己看题吧,懒得解释 思路 第一部分就是扩展欧几里德 接下来是根据 $ x=x_0+kb', y=y_0- ...

  2. Django初学习程序大致流程

  3. CSU 1374 Restore Calculation 数位DP

    题意: 给你三个数A, B, C(没有前导0),但是其中某些位不知道. 问A+B=C成立有多少种情况. 思路: 从最后一位往前推,枚举A, B的每一种情况,考虑进位和不进位两种情况. 代码: #inc ...

  4. cocos2d-js导弹跟踪算法(一边追着目标移动一边旋转角度)

    跟踪导弹 function(targetPosition){ // 让物体朝目标移动的方法 ; var targetPoint = targetPosition; var thisPoint = cc ...

  5. UvaLive 6600 Spanning trees in a secure lock pattern 矩阵行列式

    链接:https://icpcarchive.ecs.baylor.edu/index.php? option=com_onlinejudge&Itemid=8&page=show_p ...

  6. Android - TextureView, SurfaceView和GLSurfaceView 以及 SurfaceTexture

    这几个概念比较绕, 又比较相近. 初看比较糊涂, 把握关键点就好. 关键字 View SurfaceViewGLSurfaceViewTextureView这三个后缀都是View, 所以这三个东西都是 ...

  7. node --- 服务一直启动

    使用node xxx.js命令可以开始在服务器运行node.js程序. 可是它会占用终端的当前进程,而且当你离开服务器连接的时候(e.g.关闭终端或者Putty) node.js程序也会退出. 如何让 ...

  8. Linux常用系统管理软件

    1.BleachBit是一款开源的系统清理工具,它可以释放磁盘空间,保护您的隐私,清除缓存,删除cookies.internet历史.临时文件.日志和丢弃的垃圾文件等,支持清除应用的残留数据,切碎文件 ...

  9. Vue总结(一)

    vue总结 构建用户界面的渐进式框架 渐进式:用到什么功能即可使用转么的框架子模块. 两个核心点 向应的数据绑定 当时图发生改变->自动跟新视图,利用Object.defindProperty中 ...

  10. Node书签

    1.开源项目 [译]过去一年25个惊人的开源Node.js项目(2018版) 百度网盘下载助手