[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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2014-11-03 [Angular-Scaled web] 2. Architecture sub-modules
2014-11-03 [Angular-Scaled web] 1. Architecture and file structure
2014-11-03 [Big Data] Week4B (Basic)