Skip to content

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 ​

bash
npm install @angular-redux2/decorators
bash
pnpm add @angular-redux2/decorators

It 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:

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:

ts
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:

ts
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:

ts
@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 ​

ExportUse
@SelectAn observable of a slice of the state
@Select$A selection piped through an RxJS transformer
@SelectSignalA signal of a slice of the state
@DispatchDispatches the return value of a method
@SubstoreScopes the members of a class to a sub-store
provideNgReduxDecoratorsHands the store to the decorators
SelectorType, ComparatorType, TransformerTypeThe 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:

ts
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.

See also ​

Released under the Mozilla Public License 2.0