angular - @ngrx 出现“NullInjectorError : No provider for t!”错误

标签 angular ngrx ngrx-effects

我有一个使用 ngrx 12.0.0angular 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)

enter image description here

我已经正确设置了 ngrx效果。请参阅下面显示效果的代码。 UserService 是模块中provider 中的一个条目。

该错误并未说明缺少哪个提供者。这不是生产版本,而是在我的笔记本电脑中进行测试的版本。

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)))
          );
        })
      );
    });
}

最佳答案

如果你没有提供某些东西或者你没有在模块上调用 forRoot() 方法,通常会抛出错误“No Provider for xyz”

在你的情况下,检查你是如何声明效果的。下面是它的外观示例

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

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

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

关于angular - @ngrx 出现“NullInjectorError : No provider for t!”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67881270/

相关文章:

angular - $q.all() 和 $q.race() 相当于 Angular 2 中的 Observables

angular - 在 Angular 8 中使用动态表单创建复选框

rxjs - 延迟测试 NGRX 效果

angular - 限制文本输入值的正确方法(例如,仅限数字)

typescript - NgRx createAction 方法返回类型签名的含义

angular - 来自 ngrx/entity 的 SelectAll 没有选择任何东西

angular - 对象 Angular 2+ ngrx 中的嵌套数组

javascript - 为什么 @ngrx/effects 使用嵌套可观察量

javascript - 如何在 Angular2 应用程序中使用 ngrx-effects 从商店返回分页数据或从 api 服务返回新数据?

node.js - Angular 4 - 检测 http 请求的连接问题