微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

'NullInjectorError:t 没有提供者!' @ngrx 出错

如何解决'NullInjectorError:t 没有提供者!' @ngrx 出错

我有一个使用 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”

就您而言,请检查您是如何声明 Effect 的。下面是它应该是什么样子的示例

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

如果您的功能延迟加载

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。