[Angular] Introduce to NGXS
Went though tow youtube videos about NGXS
- https://angularfirebase.com/lessons/ngxs-quick-start-angular-state-management/
- https://www.youtube.com/watch?v=SfiO3bDUK7Q
The main thing which I am interested about is whether it can achieve the same effect as NGRX.
Haven't quite get fianl answer yet, but it looks very promising.
1. It is simple.
It has the same partten as redux-like tools.
For example, in the component, you can do:
constructor(private store: Store) {
}
addAnimal(name: string) {
this.store.dispatch(
// dispatch an action here
).subscribe(() => {
this.name.nativeElement.value = ''; // clean the input field
});
}
It return an Observable, so you can subscribe it and do anything house keeping stuff here.
Action creator:
export class AddAnimal {
static readonly type = '[Zoo] Add Animals';
constructor(public payload: string) {}
}
Notice here, it is using static function.
But NGXS doesn't have reducer concept, and it doesn't have effect class as well. But I feel this also simply the code a lot. In NGXS, all you need is something called state file:
import {Action, Selector, State, StateContext} from '@ngxs/store';
import {AddAnimal, RemoveAnimal} from './animals.action';
import {ZooService} from './zoo.service';
// Define the state Model interface
export interface ZooStateModel {
animals: string[];
loading: boolean;
}
// Define the State
@State<ZooStateModel>({
name: 'zoo',
defaults: {
animals: [],
loading: false
}
})
export class ZooState {
// You are able to use DI in state
constructor(private zooService: ZooService) {
}
// Memory selector, for public access the state
@Selector()
static getAllAnimals(state: ZooStateModel) {
return state.animals;
}
@Selector()
static isLoading( state: ZooStateModel){
return state.loading;
}
// Async action
@Action(AddAnimal)
addAnimal({getState, setState, patchState, dispatch}: StateContext<ZooStateModel>, {payload}: AddAnimal) {
const state = getState();
setState({
animals: [...state.animals, payload],
loading: true
});
this.zooService.addAnimal(payload)
.then(() => {
patchState({
loading: false
});
})
.catch((res) => {
const newState = getState();
setState({
animals: newState.animals.filter(name => name !== payload),
loading: false
});
});
}
}
- You are able to ui Angular DI inside state file. This is a huge advantage.
- Actions are no longer sync, it can be async!
- We are still able to use Selector, it works as interal(state) to exteral(component) connect. Notice that Selector are also static method
// Inside component you can do:
export class ZooComponent implements OnInit { ... @Select(ZooState.getAllAnimals) animals$: Observable<any>;
@Select(ZooState.isLoading) loading$: Observable<boolean>; constructor(private store: Store) {
} }
4. If you need more than one State working together. it is also easy to achieve, just inject Store to constructor().
import {Action, Selector, State} from '@ngxs/store';
interface SelectStateModel {
id: number;
}
export class SetSelected {
static readonly type = '[Select] Set Selected';
constructor(public payload: number) {}
}
@State<SelectStateModel>({
name: 'selected',
defaults: {
id: null
}
})
export class SelectState {
@Action(SetSelected)
setSelected({patchState}, {payload}: SetSelected) {
patchState({
id: payload
});
}
@Selector()
static getSelectedId(state: SelectStateModel) {
return state.id;
}
}
export class ZooState {
constructor(private zooService: ZooService, private store: Store) {
}
@Action(AddAnimal)
addAnimal(name: string) {
// to get current selectedId from other state
const currentId$ = this.store.select(SelectState.getSelectedId);
}
...
}
[Angular] Introduce to NGXS的更多相关文章
- [Angular 2] Understanding OpaqueToken
When using provider string tokens, there’s a chance they collide with other third-party tokens. Angu ...
- 阿里云 Angular 2 UI框架 NG-ZORRO介绍
说明: Angular2出来后,一直想找个基于Angular2的前端后台管理框架,但一直没有找到比较适合的.前段时间在Angular官网资源无意之间看到NG-ZORRO,NG-ZORRO由阿里计算平台 ...
- ABP 教程文档 1-1 手把手引进门之 AngularJs, ASP.NET MVC, Web API 和 EntityFramework(官方教程翻译版 版本3.2.5)含学习资料
本文是ABP官方文档翻译版,翻译基于 3.2.5 版本 转载请注明出处:http://www.cnblogs.com/yabu007/ 谢谢 官方文档分四部分 一. 教程文档 二.ABP 框架 三. ...
- [转载]前端——实用UI组件库
https://www.cnblogs.com/xuepei/p/7920888.html Angular UI 组件 ngx-bootstrap 是一套Bootstrap 组件 官网:https:/ ...
- 【转】前端——实用UI组件库
Angular UI 组件 ngx-bootstrap 是一套Bootstrap 组件 官网:https://valor-software.com/ngx-bootstrap/#/ github: h ...
- 前端——实用UI组件库
Angular UI 组件 ngx-bootstrap 是一套Bootstrap 组件 官网:https://valor-software.com/ngx-bootstrap/#/ github: h ...
- [Angular 2] More on *ngFor, @ContentChildren & QueryList<>
In previous artical, we introduce the how to use *ngFor. The limitation for previous solution to dis ...
- [AngularJS - thoughtram] Exploring Angular 1.3: Binding to Directive Controllers
The post we have: http://www.cnblogs.com/Answer1215/p/4185504.html gives a breif introduce about bin ...
- [Angular 2] Component relative paths
Oingial aritial --> Link Take away: import { Component, OnInit } from '@angular/core'; @Component ...
随机推荐
- CentOs7 修改rpm安装背景图
http://bbs.chinaunix.net/thread-4166176-1-1.html
- codevs1688 求逆序对(权值线段树)
1688 求逆序对 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 给定一个序列a1,a2,…, ...
- 微信小程序左右滑动切换页面示例代码--转载
微信小程序——左右滑动切换页面事件 微信小程序的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend. 这三个事件最重要的属性是pageX和pageY,表示X, ...
- H265
H265 h265 一.名词 CTU: 编码树单元 CU: 编码单元 PU: 以CU为根,对CU进行划分,一个预测单元PU包含一个亮度预测块PB和两个色度预测块PB. TU: 以CU为根,变换单元T ...
- WIN10 64位下VS2015 C#直接添加 halcon 12导出的CS文件实现视觉检测
C# halcon 12 联合编程的 实例 1.先调试好halcon程序,我以读取图片的程序为例.
- Redmine使用指南
公司之前使用JIRA登bug,但是客户在美国,他们习惯于用Redmine登bug,所以我们也开始在Redmine登bug,找来一个比较全面的Redmine使用指南,不懂时直接查看. http://bl ...
- VTK初始化New返回Null问题
原文链接:http://www.cppblog.com/mythma/archive/2013/08/02/vtk-6-new-null.html 在使用VTK6.0时候,会遇到X::New()返回为 ...
- DataGridView 单击赋值
void dataGridView1_Click(object sender, EventArgs e) { M_int_judge = ; btnSave.Enabled = true; btnSa ...
- sql server 查询数据判断为空
and xxx is NOT null and xxx is null
- Spring 团队开源 nohttp,尽可能不用 HTTP
Spring 团队开源 nohttp 项目,用以查找.替换和阻止 http:// 的使用. 项目是为了在可能使用 https:// 的情况下不使用到 http://,确保不会发生中间人攻击.Sprin ...