[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(); ...
随机推荐
- HTML学习----------DAY1 第三节
本章通过实例向您演示最常用的 HTML 标签. 提示:不要担心本章中您还没有学过的例子,您将在下面的章节中学到它们. 提示:学习 HTML 最好的方式就是边学边做实验.我们为您准备了很好的 HTML ...
- vue2.0-elementUI
main.js import Vue from 'vue' import App from './App.vue' import ElementUI from 'element-ui' import ...
- Android Support Library 23.2用法简析
写在前面的几句话 前几天谷歌发布了android-support-library-23.2支持库,这一次23.2版本增加了一些新的支持库以及新的功能.接下来这篇文章,就是对这些新功能部分做简单的用法介 ...
- centos7 安装好python3 yum报错
解决方法: 修改两个地方 vi /usr/bin/yum 将最前面的改为#! /usr/bin/python2 vi /usr/libexec/urlgrabber-ext-down #! /usr/ ...
- BZOJ 1305 二分+网络流
思路: 建图我根本没有想到啊--. (我是不会告诉你我借鉴了一下题解的思路) 把每个人拆成喜欢的和不喜欢的点 男 喜欢 向 男 不喜欢 连 边权为k的边 如果男喜欢女 那么 男喜欢向 女喜欢 连 1 ...
- Thinkpad Access Connections实现快速的在各种网络间进行切换
Thinkpad Access Connections快速的在各种网络间进行切换 因为工作的原因要经常在多个无线或有线之间切换,每次要切换到不同的固定IP地址的网络更是一种折磨,换一次就要从新输入一次 ...
- 打印机共享为什么老是出现“操作无法完成(错误 0X00000709)。再次检查打印机名称、并确保打印机连接网络
这个情况应该是访问IP连接打印机才会出现的.解决办法:不使用IP访问,使用网上邻居找计算机名称再连接打印机即可. ------------------------------------------- ...
- C/C++(C++类与对象)
构造器(constructor) 1.与类名相同,无返回,被系统生成对象时自动调用,用于初始化. 2.可以有参数,构造器的重载,有默认参数.重载和默认参数不能同时出现,但是一定要包含标配(无参数的构造 ...
- 用Understand阅读 VS2010项目源码
一.查看vs2010项目 打开understand,File-New-Project...-Next-Next [向导第三步,选"Import Visual Sudio project fi ...
- codeforces 543 C Remembering Strings
题意:若一个字符串集合里的每一个字符串都至少有一个字符满足在i位上,仅仅有它有,那么这个就是合法的,给出全部串的每一个字符修改的花费,求变成合法的最小代价. 做法:dp[i][j].前i个串的状态为j ...