store

RxJS powered state management for Angular applications, inspired by Redux

  • 所有者: ngrx/store
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图


This repository is for version 2.x of @ngrx/store.

Click here for the latest version (4.x)


@ngrx/store

RxJS powered state management for Angular applications, inspired by Redux

Join the chat at https://gitter.im/ngrx/store
CircleCI Status for ngrx/store
npm version

@ngrx/store is a controlled state container designed to help write performant, consistent applications
on top of Angular. Core tenets:

  • State is a single immutable data structure
  • Actions describe state changes
  • Pure functions called reducers take the previous state and the next action to compute the new state
  • State accessed with the Store, an observable of state and an observer of actions

These core principles enable building components that can use the OnPush change detection strategy
giving you intelligent, performant change detection
throughout your application.

Installation

Install @ngrx/core and @ngrx/store from npm:

npm install @ngrx/core @ngrx/store@2.2.3 --save

Optional packages:

Examples

  • Official @ngrx/example-app is an officially maintained example application showcasing best practices
    for @ngrx projects, including @ngrx/store and @ngrx/effects
  • angular-webpack2-starter is a complete Webpack 2 starter with built-in support for @ngrx.
    Includes Ahead-of-Time (AOT) compilation, hot module reloading (HMR), devtools, and server-side rendering.

Introduction

Setup

Create a reducer function for each data type you have in your application. The combination of these reducers will
make up your application state:

// counter.ts
import { ActionReducer, Action } from '@ngrx/store';

export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
export const RESET = 'RESET';

export function counterReducer(state: number = 0, action: Action) {
	switch (action.type) {
		case INCREMENT:
			return state + 1;

		case DECREMENT:
			return state - 1;

		case RESET:
			return 0;

		default:
			return state;
	}
}

In your app's main module, import those reducers and use the StoreModule.provideStore(reducers)
function to provide them to Angular's injector:

import { NgModule } from '@angular/core'
import { StoreModule } from '@ngrx/store';
import { counterReducer } from './counter';

@NgModule({
  imports: [
    BrowserModule,
    StoreModule.provideStore({ counter: counterReducer })
  ]
})
export class AppModule {}

You can then inject the Store service into your components and services. Use store.select to
select slice(s) of state:

import { Store } from '@ngrx/store';
import { INCREMENT, DECREMENT, RESET } from './counter';

interface AppState {
  counter: number;
}

@Component({
	selector: 'my-app',
	template: `
		<button (click)="increment()">Increment</button>
		<div>Current Count: {{ counter, async }}</div>
		<button (click)="decrement()">Decrement</button>

		<button (click)="reset()">Reset Counter</button>
	`
})
class MyAppComponent {
	counter: Observable<number>;

	constructor(private store: Store<AppState>){
		this.counter = store.select('counter');
	}

	increment(){
		this.store.dispatch({ type: INCREMENT });
	}

	decrement(){
		this.store.dispatch({ type: DECREMENT });
	}

	reset(){
		this.store.dispatch({ type: RESET });
	}
}

Contributing

Please read contributing guidelines here.

主要指标

概览
名称与所有者ngrx/store
主编程语言TypeScript
编程语言TypeScript (语言数: 2)
平台
许可证MIT License
所有者活动
创建于2015-12-13 02:44:59
推送于2017-11-27 12:55:33
最后一次提交2017-08-10 13:55:25
发布数24
最新版本名称v2.2.3 (发布于 2017-07-09 13:25:05)
第一版名称v1.0.2 (发布于 2015-12-13 19:37:35)
用户参与
星数3.9k
关注者数198
派生数311
提交数151
已启用问题?
问题数359
打开的问题数1
拉请求数49
打开的拉请求数9
关闭的拉请求数49
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?