Getting Started
Version 6 of @angular-redux2/store replaced the decorators with signal APIs such as injectSelect and injectSubstore. This package brings the 5.x decorators back on top of the version 6 store, so existing components keep working while you migrate them one at a time.
🧭 New code
Write new components with the signal APIs. See Migrating from 5.x for why the decorators left the core and how each one maps to a signal.
Installation
npm install @angular-redux2/decoratorspnpm add @angular-redux2/decoratorsIt needs @angular-redux2/store 6, Angular 20 or newer, RxJS 7.5 or newer, and Redux 5.
Configure TypeScript
The decorators define getters on the class prototype, so they need two settings in tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false
}
}⚙️ useDefineForClassFields
A new Angular project targets ES2022, where useDefineForClassFields defaults to true. The class field then hides the getter, and every decorated property reads undefined without an error.
Provide the store
Add provideNgReduxDecorators() next to provideNgRedux:
import { isDevMode, type ApplicationConfig } from '@angular/core';
import { provideNgRedux } from '@angular-redux2/store';
import { provideNgReduxDecorators } from '@angular-redux2/decorators';
export const appConfig: ApplicationConfig = {
providers: [
provideNgRedux({ reducer: rootReducer, devTools: isDevMode() }),
provideNgReduxDecorators()
]
};A decorator runs outside Angular's injection context, so it cannot call inject. The provider hands the store to the decorators when the application starts and takes it back when the application is destroyed. A decorated member read before that throws an error that names the missing provider.
Change the import
The component stays as it was. Only the import changes:
import type { Observable } from 'rxjs';
import { Component } from '@angular/core';
import { Dispatch, Select } from '@angular-redux2/decorators';
import { Dispatch, Select } from '@angular-redux2/store';
@Component({
selector: 'app-counter',
template: `
<p>{{ count$ | async }}</p>
<button (click)="increment()">+1</button>
`
})
export class Counter {
@Select('counter') readonly count$: Observable<number>;
@Dispatch
increment() {
return { type: 'INCREMENT' };
}
}A decorated member and a signal selection share one store, so both styles can sit in one component:
@Component({ template: '{{ count$ | async }} / {{ doubled() }}' })
export class Mixed {
@Select('counter') readonly count$: Observable<number>; // 5.x style
readonly doubled = injectSelect((state: AppState) => state.counter * 2); // 6.x style
}What it exports
| Export | Use |
|---|---|
@Select | An observable of a slice of the state |
@Select$ | A selection piped through an RxJS transformer |
@SelectSignal | A signal of a slice of the state |
@Dispatch | Dispatches the return value of a method |
@Substore | Scopes the members of a class to a sub-store |
provideNgReduxDecorators | Hands the store to the decorators |
SelectorType, ComparatorType, TransformerType | The 5.x selector, comparator, and transformer types |
The 5.x class reducers (AbstractReducer and @Action) are not part of this package. Move them to createSlice.
Testing
Add provideNgReduxDecorators() after provideMockNgRedux, and the decorators read the mock:
import { TestBed } from '@angular/core/testing';
import { MockNgRedux, provideMockNgRedux } from '@angular-redux2/store/mocks';
import { provideNgReduxDecorators } from '@angular-redux2/decorators';
TestBed.configureTestingModule({
providers: [ provideMockNgRedux({ counter: 1 }), provideNgReduxDecorators() ]
});
const fixture = TestBed.createComponent(Counter);
fixture.componentInstance.increment();
TestBed.inject(MockNgRedux).dispatched; // [ { type: 'INCREMENT' } ]Each test bed hands over its own store, so one test never reads the store of another.
Removing the package
Move each component to the signal APIs, then delete the package, the provider, and "useDefineForClassFields": false. The signal APIs need none of them.
