actions$ not injected in constructor of effect

Discussão em 'Angular' iniciado por Simone Buscaino, Novembro 20, 2024.

  1. I use NGRX 18 and Angular 18, I am trying to inject actions$ into my effect via constructor, but this doesn't work

    import { inject, Injectable } from "@angular/core";
    import { Actions, createEffect, ofType } from "@ngrx/effects";
    import { actionsList } from "./students.actions";
    import { catchError, exhaustMap, map, of } from "rxjs";
    import { StudentsRecordsService } from "../services/students-records.service";

    @Injectable()
    export class StudentsRecordsEffects {
    // actions$ = inject(Actions);
    // studentsRecordsService = inject(StudentsRecordsService);

    loadStudentsRecords$ = createEffect(() => this.actions$.pipe(
    ofType(actionsList.callStudentsRecordsApi),
    exhaustMap(() => this.studentsRecordsService.getStudentsRecords().pipe(
    map(studentsRecords => {
    console.log('API Response:', studentsRecords); // Debug API Response
    return { type: actionsList.callStudentsRecordsApiSuccess, payload: studentsRecords }
    }),
    catchError((error) => {
    console.error('Error fetching students records:', error); // Debug Error
    return of({ type: '[Students API] Students Loaded Error' });
    }),
    ))
    ));

    constructor(
    private actions$: Actions,
    private studentsRecordsService: StudentsRecordsService
    ) {}
    }


    My error in console:

    main.ts:6 TypeError: Cannot read properties of undefined (reading 'pipe')
    at students.effects.ts:12:59
    at <instance_members_initializer> (students.effects.ts:12:26)
    at new _StudentsRecordsEffects (students.effects.ts:26:3)
    at Object.StudentsRecordsEffects_Factory [as factory] (students.effects.ts:31:3)


    If I try to inject actions$ with the inject() method instead, everything works correctly.

    What's wrong?

    Continue reading...

Compartilhe esta Página