Quick Start

app.config.ts

import {provideStore} from '@ngxs/store';
import {provideNgxsDataPlugin} from '@angular-ru/ngxs';

export const appConfig: ApplicationConfig = {
  providers: [
    // ..
    provideStore([AppState]),
    provideNgxsDataPlugin(),
  ],
};

count.state.ts

import {NgxsDataRepository} from '@angular-ru/ngxs/repositories';
import {Computed, DataAction, StateRepository} from '@angular-ru/ngxs/decorators';
import {State} from '@ngxs/store';
// ..

export interface CountModel {
  val: number;
}

@StateRepository()
@State({
  name: 'count',
  defaults: {val: 0},
})
@Injectable()
export class CountState extends NgxsDataRepository<CountModel> {
  @Computed()
  public get values$() {
    return this.state$.pipe(map((state) => state.countSub));
  }

  @DataAction()
  public increment(): void {
    this.ctx.setState((state) => ({val: state.val + 1}));
  }

  @DataAction()
  public decrement(): void {
    this.ctx.setState((state) => ({val: state.val - 1}));
  }

  @Debounce()
  @DataAction()
  public setValueFromInput(@Payload('value') val: string | number): void {
    this.ctx.setState({val: parseFloat(val) || 0});
  }
}

app.component.ts

Debugging

Can be debugged using NGXS logger plugin.

Last updated