@angular-redux2/decorators brings the 5.x @Select, @Select$, @SelectSignal, @Dispatch, and @Substore
decorators back on top of the @angular-redux2/store 6 signal store.
Decorated components keep working after the upgrade, and you move them to the signal APIs one at a time.
- Change one import: swap
@angular-redux2/storefor@angular-redux2/decoratorsin the import, and a 5.x component compiles and runs on version 6. - Side by side with signals: a decorated member and an
injectSelectselection share one store, so both styles can live in one application, or in one component. - Free after the first read: a decorated property builds its selection once and then becomes a plain property, so a template that reads it on every change detection pass pays for the decorator once.
- No global store:
provideNgReduxDecorators()hands the store of the application to the decorators and takes it back when the application is destroyed, so one test never reads the store of another. - Sub-stores that follow the path:
@Substorecreates one sub-store per instance whose path followsbasePath(), and releases it inngOnDestroy. - Testing mocks: add the provider after
provideMockNgRedux, and the decorators read the mock.
npm install @angular-redux2/decoratorsRequires @angular-redux2/store 6, Angular 20 or newer, RxJS 7.5 or newer, and Redux 5.
The decorators define getters on the class prototype, so they need this in tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false
}
}Without "useDefineForClassFields": false, the class field hides the getter, and every decorated property silently
reads undefined.
import { provideNgRedux } from '@angular-redux2/store';
import { provideNgReduxDecorators } from '@angular-redux2/decorators';
import { type ApplicationConfig, isDevMode } from '@angular/core';
export const appConfig: ApplicationConfig = {
providers: [
provideNgRedux({ reducer: rootReducer, devTools: isDevMode() }),
provideNgReduxDecorators()
]
};import type { Observable } from 'rxjs';
import type { Signal } from '@angular/core';
import { Component } from '@angular/core';
import { Dispatch, Select, SelectSignal } from '@angular-redux2/decorators';
@Component({
selector: 'app-counter',
template: '<button (click)="increment()">{{ count$ | async }} / {{ name() }}</button>'
})
export class CounterComponent {
@Select('counter') readonly count$: Observable<number>;
@SelectSignal([ 'user', 'name' ]) readonly name: Signal<string>;
@Dispatch
increment() {
return { type: 'INCREMENT' };
}
}import { Dispatch, Select, Substore } from '@angular-redux2/decorators';
@Component({ template: '{{ loc$ | async }}' })
@Substore(userReducer)
export class UserComponent {
@Input() userId: string;
@Select('loc') readonly loc$: Observable<number>;
basePath = () => this.userId ? [ 'users', this.userId ] : null;
@Dispatch
addCode(lines: number) {
return { type: 'ADD_LOC', payload: lines };
}
}| Decorator | Does |
|---|---|
@Select |
An observable of a key, a path, or a function of the state |
@Select$ |
A selection piped through an RxJS transformer |
@SelectSignal |
A signal of a key, a path, or a function of the state |
@Dispatch |
Dispatches the return value of a method, unless it returns false |
@Substore |
Scopes the members of a class to a sub-store at basePath() |
This package is a bridge. Write new components with injectSelect, injectSubstore, and inject(NgRedux), and once
the last decorated component is migrated, remove the package, the provider, and "useDefineForClassFields": false.
See Migrating from 5.x for why the decorators left the core and a side-by-side guide for each one.
Full guides and the complete API reference live at angular-redux2.github.io/decorators.
Contributions are welcome!
Please see our Contributing Guide for details.
Documentation, GitHub Repository, Issue Tracker, npm Package
This project is licensed under the Mozilla Public License 2.0 - see the LICENSE file for details.