返回排行榜

ngrx/store

TypeScript

RxJS powered state management for Angular applications, inspired by Redux

ngrxangularrxjsredux
Star 增长趋势
Star
3.9k
Forks
308
周增长
Issues
1
1k2k3k
2015年12月2019年6月2023年1月2026年7月
制品库npmnpm install store
README

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/platform

Reactive State for Angular

TypeScriptnpmOtherngrxrxjs
ngrx.io
8.3k2k
angular/angularfire

Angular + Firebase = ❤️

TypeScriptnpmMIT Licensefirebaseangular
firebaseopensource.com/projects/angular/angularfire2
7.8k2.2k
altair-graphql/altair

✨⚡️ A feature-rich GraphQL Client for all platforms.

TypeScriptnpmMIT Licensegraphqlgraphql-client
altairgraphql.dev
5.4k402
timjacobi/angular-education

A list of helpful material to develop using Angular

angularangular2-components
5.3k853
ngxs/store

🚀 NGXS - State Management for Angular

TypeScriptnpmMIT Licensecqrsredux
ngxs.io
3.5k404
tomastrajan/angular-ngrx-material-starter

Angular, NgRx, Angular CLI & Angular Material Starter Project

TypeScriptnpmMIT Licenseangularngrx
tomastrajan.github.io/angular-ngrx-material-starter
2.9k902
trungvose/angular-spotify

Spotify client built with Angular 15, Nx Workspace, ngrx, TailwindCSS and ng-zorro

TypeScriptnpmMIT Licenseangularnx
spotify.trungk18.com
2.8k418
NathanWalker/angular-seed-advanced

Advanced Angular seed project with support for ngrx/store, ngrx/effects, ngx-translate, angulartics2, lodash, NativeScript (*native* mobile), Electron (Mac, Windows and Linux desktop) and more.

TypeScriptnpmMIT Licenseangularnativescript
2.2k432
sysgears/apollo-universal-starter-kit

Apollo Universal Starter Kit is a SEO-friendly, fully-configured, modular starter application that helps developers to streamline web, server, and mobile development with cutting-edge technologies and ultimate code reuse.

JavaScriptnpmMIT Licensegraphqlapollo
apollokit.org
1.7k316
TrilonIO/aspnetcore-angular-universal

ASP.NET Core & Angular Universal advanced starter - PWA w/ server-side rendering for SEO, Bootstrap, i18n internationalization, TypeScript, unit testing, WebAPI REST setup, SignalR, Swagger docs, and more! By @TrilonIO

TypeScriptnpmMIT Licenseangularuniversal
trilon.io
1.4k421
tomalaforge/angular-challenges

Set of Angular challenges to practise and train on Angular

TypeScriptnpmangularchallenge
angular-challenges.vercel.app
1.4k2.9k
ngrx/example-app

Example app showcasing the ngrx platform

TypeScriptnpmMIT Licensengrxangular
1.4k484