@angular-redux-ivy/store

中文文档
13.0.4发布于 2022/09/26MITTypeScriptESM + CJS近 30 天下载 3438

Angular bindings for Redux

原始使用文档
文档目录13 节
  1. Angular 6+
  2. Angular 5
  3. Angular 4 or lower
  4. What is Redux?
  5. What is @angular-redux?
  6. Getting Started
  7. Installation
  8. Quick Start
  9. Examples
  10. Companion Packages
  11. Resources
  12. In-Depth Usage
  13. Hacking on angular-redux/store

:warning: This is a fork of the original project

It enables Ivy and makes it work with Angular 13+

Which Version to use?

Angular 6+

@angular-redux/store@^9 is what you need. This consumes breaking changes from RxJS and Angular 6, as well as updated typedefs from Redux 4.

Angular 5

Use @angular-redux/store@^7 - this version supports Angular 5, and also changes to using lettable operators.

Angular 4 or lower

Use @angular-redux/store@^6 - This supports Angular 4 and earlier.

Support for @angular-redux/store@6?

Where possible, I will be maintaining and applying any fixes / enhancements for v7 into v6 where it does not introduce a breaking change.

I made a few mistakes trying to publish fixes / etc to two major versions, which caused some releases to get tagged incorrectly and caused some confusion. Sorry for any confusion this has caused, and will do better on avoiding this in the future, and being more transparent with the releases that are going out.

@angular-redux/store

Angular bindings for Redux.

For Angular 1 see ng-redux

Join the chat at https://gitter.im/angular-redux/ng2-redux CircleCI npm version downloads per month

What is Redux?

Redux is a popular approach to managing state in applications. It emphasises:

  • A single, immutable data store.
  • One-way data flow.
  • An approach to change based on pure functions and a stream of actions.

You can find lots of excellent documentation here: Redux.

What is @angular-redux?

We provide a set of npm packages that help you integrate your redux store into your Angular 2+ applications. Our approach helps you by bridging the gap with some of Angular's advanced features, including:

  • Change processing with RxJS observables.
  • Compile time optimizations with NgModule and Ahead-of-Time compilation.
  • Integration with the Angular change detector.

Getting Started

Installation

@angular-redux/store has a peer dependency on redux, so we need to install it as well.

npm install --save redux @angular-redux/store

Quick Start

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './containers/app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

Import the NgReduxModule class and add it to your application module as an import. Once you've done this, you'll be able to inject NgRedux into your Angular components. In your top-level app module, you can configure your Redux store with reducers, initial state, and optionally middlewares and enhancers as you would in Redux directly.

import { NgReduxModule, NgRedux } from '@angular-redux/store';
import { createLogger } from 'redux-logger';
import { rootReducer } from './reducers';

interface IAppState {
  /* ... */
}

@NgModule({
  /* ... */
  imports: [, /* ... */ NgReduxModule],
})
export class AppModule {
  constructor(ngRedux: NgRedux<IAppState>) {
    ngRedux.configureStore(rootReducer, {}, [createLogger()]);
  }
}

Or if you prefer to create the Redux store yourself you can do that and use the provideStore() function instead:

import {
  applyMiddleware,
  Store,
  combineReducers,
  compose,
  createStore,
} from 'redux';
import { NgReduxModule, NgRedux } from '@angular-redux/store';
import { createLogger } from 'redux-logger';
import { rootReducer } from './reducers';

interface IAppState {
  /* ... */
}

export const store: Store<IAppState> = createStore(
  rootReducer,
  applyMiddleware(createLogger()),
);

@NgModule({
  /* ... */
  imports: [, /* ... */ NgReduxModule],
})
class AppModule {
  constructor(ngRedux: NgRedux<IAppState>) {
    ngRedux.provideStore(store);
  }
}

Note that we're also using a Redux middleware from the community here: redux-logger. This is just to show off that @angular-redux/store is indeed compatible with Redux middlewares as you might expect.

Note that to use it, you'll need to install it with npm install --save redux-logger and type definitions for it with npm install --save-dev @types/redux-logger.

Now your Angular app has been reduxified! Use the @select decorator to access your store state, and .dispatch() to dispatch actions:

import { select } from '@angular-redux/store';

@Component({
  template:
    '<button (click)="onClick()">Clicked {{ count | async }} times</button>',
})
class App {
  @select() count$: Observable<number>;

  constructor(private ngRedux: NgRedux<IAppState>) {}

  onClick() {
    this.ngRedux.dispatch({ type: INCREMENT });
  }
}

Examples

Here are some examples of the angular-redux family of packages in action:

Companion Packages

Resources

In-Depth Usage

@angular-redux/store uses an approach to redux based on RxJS Observables to select and transform data on its way out of the store and into your UI or side-effect handlers. Observables are an efficient analogue to reselect for the RxJS-heavy Angular world.

Read more here: Select Pattern

We also have a number of 'cookbooks' for specific Angular topics:

Hacking on angular-redux/store

Want to hack on angular-redux/store or any of the related packages? Feel free to do so, but please test your changes before making any PRs.

Here's how to do that:

  1. Write unit tests. You can check that they work by running npm test.
  2. Run the linter. If your editor doesn't do it automatically, do it manually with npm run lint.
  3. Test your changes in a 'real world scenario'. We use the example-app for this, using some npm fakery to 'publish the package locally':
  • clone the example app (git clone https://github.com/angular-redux/example-app.git)
  • generate a 'local package' (cd to your angular-redux/store clone and run npm pack). This will create a .tgz file.
  • hook your 'local package' up to your example-app (cd to your example-app clone and run npm install --save /path/to/the/tgz/file/from/above)
  • run ng serve --aot

Please make sure your changes pass Angular's AoT compiler, because it's a bit finicky with TS syntax.

版本与发布记录

@angular-redux-ivy/store 共发布 4 个版本,首次发布于 2022/08/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
13.0.4latest2022/09/264 年前
13.0.32022/09/264 年前
13.0.12022/08/254 年前
13.0.02022/08/254 年前

关于 @angular-redux-ivy/store 的常见问题

@angular-redux-ivy/store 是什么?

Angular bindings for Redux

如何安装 @angular-redux-ivy/store?

在项目目录运行 npm install @angular-redux-ivy/store,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/core(^7.0.0)、redux(^4.0.0)、rxjs(^6.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@angular-redux-ivy/store 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@angular-redux-ivy/store 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@angular-redux-ivy/store 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=8。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@angular-redux-ivy/store 可以商用吗?

@angular-redux-ivy/store 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@angular-redux-ivy/store 的下载量有多少?

根据 npm 官方下载统计,@angular-redux-ivy/store 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,438 次,日均约 115 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@angular-redux-ivy/store 还在维护吗?

最新版本 13.0.4 发布于 2022/09/26(4 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/08/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。