[Angular] How to get Store state in ngrx Effect
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的更多相关文章
- [Angular] NgRx/effect, why to use it?
See the current implementaion of code, we have a smart component, and inside the smart component we ...
- 用computed返回this.$store.state.count,store更改了,但是computed没有调用
今天出现了这个问题,store更新了,你computed为啥不调用呢??? 另一个.vue更新了state,这个的computed就监听不到了么? 是用这种格式更新的this.$store.commi ...
- vuex this.$store.state.属性和mapState的属性中的一点点区别
做泰康公众号的项目时候有一个需求创建公众号的时候后台有一个社区id提供给后台展现人员和部门,在群发消息时候也要给后台一个社区id只不过获取社区的id接口和上一个不是一样的,本来在页面中写了两个sele ...
- Do not mutate vuex store state outside mutation handlers.
组件代码: selectItem(item,index) { this.selectPlay({ list: this.songs, index }) }, ...mapActions([ 'sele ...
- 【vue store的使用方法】(this.$store.state this.$store.getters this.$store.dispatch this.$store.commit)
vue 页面文件 <template> <div> {{this.$store.state.count}}<br/> {{count}}<br/> {{ ...
- [Angular] Creating an Observable Store with Rx
The API for the store is really simple: /* set(name: string, state: any); select<T>(name: stri ...
- [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 ...
- 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 ...
- VUEX报错 [vuex] Do not mutate vuex store state outside mutation handlers
数组 错误的写法:let listData= state.playList; // 数组深拷贝,VUEX就报错 正确的写法:let listDate= state.playList.slice(); ...
随机推荐
- 洛谷1019 单词接龙 字符串dfs
问题描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...
- MySql系列表之间的关系
foreign key 快速理解foreign key 员工信息表有三个字段:工号 姓名 部门 公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费 数据 ...
- easyui_datagrid使用
easyui的datagrid显示数据的方式(使用了jQuery) 第一步 创建显示的格式,方法有两种: 第一种:在HTML标签中创建,类似如下的形式,参数可以在标签中设置,也可以在脚本中 这种方式在 ...
- python语法学习笔记
函数的参数 定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复杂逻辑被封装起来 ...
- CodeForces 312B Archer
Archer Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ...
- 【转】C#中RSA加密解密和签名与验证的实现
[转]C#中RSA加密解密和签名与验证的实现 RSA加密算法是一种非对称加密算法.在公钥加密标准和电子商业中RSA被广泛使用.RSA是1977年由罗纳德•李维斯特(Ron Rivest).阿迪•萨莫尔 ...
- COGS——T 2478. [HZOI 2016]简单的最近公共祖先
http://www.cogs.pro/cogs/problem/problem.php?pid=2478 ★☆ 输入文件:easy_LCA.in 输出文件:easy_LCA.out 简单 ...
- Project Euler:Problem 58 Spiral primes
Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...
- hadoop MR 任务 报错 "Error: java.io.IOException: Premature EOF from inputStream at org.apache.hadoop.io"
错误原文分析 文件操作超租期,实际上就是data stream操作过程中文件被删掉了.一般是由于Mapred多个task操作同一个文件.一个task完毕后删掉文件导致. 这个错误跟dfs.datano ...
- Dubbo源代码分析(三):Dubbo之服务端(Service)
如上图所看到的的Dubbo的暴露服务的过程,不难看出它也和消费者端非常像,也须要一个像reference的对象来维护service关联的全部对象及其属性.这里的reference就是provider. ...