美文网首页
初涉angular之Ngxs状态管理

初涉angular之Ngxs状态管理

作者: 前端小慵卿 | 来源:发表于2019-07-11 12:11 被阅读0次

ngxs是一款angular的状态管理框架。他能帮助我们全局性的管理应用程序的所有状态,状态的概念无所不包,我们的数据,登录认证,等等,都可以称为状态。

ngxs架构

ngxs有四个概念:

  • Store: Global state container, action dispatcher and selector
  • Actions: Class describing the action to take and its associated metadata
  • State: Class definition of the state
  • Selects: State slice selectors

store是ngxs核心,action,selects最终将融入state类,并被注册到store里
请看下图:


安装npm

npm install @ngxs/store --save
npm install @ngxs/devtools-plugin --save
npm install @ngxs/logger-plugin --save
// app.module.ts
import { NgxsModule } from '@ngxs/store';
import { NgxsReduxDevtoolsPluginModule} from '@ngxs/devtools-plugin';
import { NgxsLoggerPluginModule } from '@ngxs/logger-plugin';
@NgModule({
 imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,
    HttpClientModule,
    NgxsModule.forRoot([
      UserState
    ]),
    NgxsReduxDevtoolsPluginModule.forRoot(),
    NgxsLoggerPluginModule.forRoot()
  ], 
...
})

官网地址:https://ngxs.gitbook.io/ngxs/

项目目录结构
store目录
action类
export class Edit {
  static readonly type = '[user] Edit';
  constructor(public payload: { userEdit: any }) {}
}
model类
export class User {
  userId: string;
  username: string;
  password: string;
}

state类
import {State, Action, Selector, StateContext} from '@ngxs/store';
import { Router } from '@angular/router';
import { NgZone } from '@angular/core';
import { Response, ResponseCode } from '../models/response.model';
import {map} from 'rxjs/operators';
import * as UserAction from '../actions/user.action';
import { User } from '../models/user.model';
import { UserService } from '../services/user.service';

export class UserStateModel {
  userInfo: User;
}

@State<UserStateModel>({
  name: 'user',
  defaults: {
    userInfo: null
  }
})

export class UserState {
  @Selector()
  static getLoginUser(state: UserStateModel) {
    return state.userInfo;
  }
  constructor(
    private router: Router,
    public ngZone: NgZone,
    private userService: UserService) {}

    @Action(UserAction.Edit)
    Edit(ctx: StateContext<UserStateModel>, action: UserAction.Edit) {
      console.log(action.payload.userEdit);
      ctx.patchState({
        userInfo : action.payload.userEdit
      });
    }
}

Component View显示 主要代码
export class HomeComponent implements OnInit {
  userInfo: any;
// 1.在component中订阅userInfo状态
  @Select(UserState.getLoginUser) userInfo$: Observable<User>;

  ngOnInit() {
// 2.subscribe订阅
    this.userInfo$.subscribe(userInfo => {
      this.userInfo = userInfo;
    });
  }
updateName() {
    this.userInfo.username = this.nameUpdate;
// 3.在component中使用dispatch
    this.store.dispatch(new Edit({
      userEdit: this.userInfo,
    }));
  }
}
<p class="home-message">恭喜{{(userInfo$ | async).username}}登录成功!</p>
<p class="home-message">{{userInfo.username}},你好!</p>

相关文章

  • 初涉angular之Ngxs状态管理

    ngxs是一款angular的状态管理框架。他能帮助我们全局性的管理应用程序的所有状态,状态的概念无所不包,我们的...

  • 初涉angular的总结

    初涉angular之自定义指令 1.自定义指令 在app.module声明指令,在app.module的decla...

  • 初涉管理

    1.自己的立场非常重要 在复杂的机关当中,每个人都有自己的立场领导有领导的立场。二级单位有二级单位的立场,但是要时...

  • Angular状态管理 - NgRx

    Angular 官方框架本身提供了较为全面的功能,但是却缺少诸如 Redux,Vuex 之类的状态管理框架,而 N...

  • 初涉时间管理

    现代人生活节奏快,片段时间都变成了碎片化。手机更是不离手,想要做点事情,却总是被小视频转走了视线。 不知道大家有没...

  • NgRX-01

    1. 介绍ngrx ngrx 相当于 Angular 版本的 redux,是 Angular 的状态管理工具 2....

  • Angular数据检测及单向数据流

    本文学习总结于 太狼 关于“angular2中数据状态管理方案有哪些?”的回答 Angular 2 Change ...

  • Angular8 中使用 mobx

    前言: 最近在折腾一个 ionic4 项目,主要是围绕着 Angular 转圈圈,状态管理这块,Angular 官...

  • 初涉.

  • 初涉

    环境造就格局预定义人生, 人们忽略换位想掌控乾坤。 骄傲而固执的人认为世间没有对错, 平静的银河系中无数的星系在黑...

网友评论

      本文标题:初涉angular之Ngxs状态管理

      本文链接:https://www.haomeiwen.com/subject/hhsrkctx.html