platform icon indicating copy to clipboard operation
platform copied to clipboard

Strange behavior on Actions observer injection when using normal constructor assignment

Open JulianRuhrDev opened this issue 7 years ago • 1 comments

Minimal reproduction of the bug/regression with instructions:

There is a strange behavior when trying to inject the Actions observable into a constructor.

For this effect the Actions observable is injected and I can use it normally across my application.

import {Injectable} from '@angular/core';
import {Actions, Effect} from '@ngrx/effects';

@Injectable()
export class WorkingEffect {

  constructor(private readonly actions$: Actions) {
    // This injection works.
  }

  @Effect()
  public test$ = this.actions$.pipe();
}

However if I choose to do the following style of constructor call the Actions observable is not injected and the effect throws an error that TypeError: Cannot read property 'pipe' of undefined.

import {Injectable} from '@angular/core';
import {Actions, Effect} from '@ngrx/effects';

@Injectable()
export class FailingEffect {

  private readonly actions$: Actions;

  constructor(actions$: Actions) {
    this.actions$ = actions$;
    // This will leave actions$ as undefined
  }

  @Effect()
  public test$ = this.actions$.pipe();
}

As far as I know both constructor styles are valid and should have the same behavior.

Expected behavior:

The expected behavior is that the classic constructor injection should also work.

import {Injectable} from '@angular/core';
import {Actions, Effect} from '@ngrx/effects';

@Injectable()
export class FailingEffect {

  private readonly actions$: Actions;

  constructor(actions$: Actions) {
    this.actions$ = actions$;
    // This will leave actions$ as undefined
  }

  @Effect()
  public test$ = this.actions$.pipe();
}

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s):

  • NgRx: 7.2.0
  • Angular: 7.1.0
  • Chrome Browser Version 71.0.3578.98 (64-Bit)
  • OS: macOS Mojave 10.14.2

Other information:

I would be willing to submit a PR to fix this issue

[ ] Yes (Assistance is provided if you need help submitting a pull request) [ ] No

JulianRuhrDev avatar Feb 08 '19 10:02 JulianRuhrDev

This is related to TS.

The following also fails:

  private readonly foo: number;
  x = this.foo.toString();

As work around you could use a factory function as:

  @Effect()
  public test$ = () => this.actions$.pipe();

timdeschryver avatar Feb 08 '19 18:02 timdeschryver