“NullInjectorError:没有 t 的提供者!” @ngrx 错误

won*_*rld 6 ngrx ngrx-effects angular

我有一个使用ngrx 12.0.0的Angular 12.0.2应用程序。当我运行应用程序并访问延迟加载模块的路由时,出现以下错误。

ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(t)[t -> InjectionToken @ngrx/effects Feature Effects -> [object Object] -> t -> t -> t -> t -> t -> t]: 
  NullInjectorError: No provider for t!
NullInjectorError: R3InjectorError(t)[t -> InjectionToken @ngrx/effects Feature Effects -> [object Object] -> t -> t -> t -> t -> t -> t]: 
  NullInjectorError: No provider for t!
    at fs.get (main.js:1)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我已经正确设置了ngrxEffects请看下面显示效果的代码。这是模块中提供UserService程序的一个条目。

该错误没有说明缺少哪个提供者。这不是生产版本,而是我的笔记本电脑中用于测试的版本。

import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Observable, EMPTY, of } from 'rxjs';
import { catchError, debounceTime, map, switchMap, withLatestFrom, concatMap } from 'rxjs/operators';
import * as UserActions from '../actions/user.actions';
import { VisitorService } from '../../service/visitor.service';
import { asyncScheduler } from 'rxjs';

@Injectable()
export class UserEffects {

  constructor(private actions$: Actions,
    private userService: UserService) { }

  loadUsers$ = createEffect(
    () => ({ debounce = 300, scheduler = asyncScheduler } = {}) => {
      return this.actions$.pipe(
        ofType(UserActions.loadUsers),
        debounceTime(debounce, scheduler),
        switchMap((_) => {
          return this.userService.getUserss().pipe(
            map(users => (UserActions.loadUsersSuccess({ data: users }))),
            catchError(err => of(UserActions.loadUsersFailure(err)))
          );
        })
      );
    });
}
Run Code Online (Sandbox Code Playgroud)

Owe*_*vin 2

如果您不提供某些内容或者不调用forRoot()模块上的方法,通常会抛出错误“No Provider for xyz”

对于您的情况,请检查您如何声明效果。下面是它应该是什么样子的示例

imports: [
  EffectsModule.forRoot([]),
]

Run Code Online (Sandbox Code Playgroud)

如果您的功能是延迟加载的

imports: [
  EffectsModule.forFeature([Feature1Effects, Feature2Effects]),
]

Run Code Online (Sandbox Code Playgroud)