79454292

Date: 2025-02-20 11:18:14
Score: 1
Natty:
Report link

I faced the same challenge and eventually found a solution.

  1. In main.ts, use provideEffects([EffectSources])
  2. Provide your effect (more convenient to provide it in root within @Injectable decorator)
  3. In your component where you need the store, inject two services: your effects and EffectSources
  4. In the constructor function of the component, invoke addEffects function from EffectSources and pass the instance of your effect

See the code snippets below

// main.ts
import { EffectSources, provideEffects } from '@ngrx/effects';

bootstrapApplication(AppComponent, {
  providers: [
    // your providers here
    provideEffects([EffectSources]),
  ]
});

// your-effect.ts
@Injectable({
    providedIn: 'root',
})
export class YourEffect {}

// your-component.ts
@Component({
  selector: 'app-selector',
  standalone: true,
})
export class YourComponent {
  constructor(
    private effectSources: EffectSources,
    private yourEffect: YourEffect,
  ) {
    effectSources.addEffects(yourEffect)
  }
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Injectable
  • Low reputation (1):
Posted by: Alex Mirankov