简体   繁体   English

如何解决 Angular 8 解析器中的“NullInjectorError: StaticInjectorError”

[英]How to troubleshoot 'NullInjectorError: StaticInjectorError' in Angular 8 resolver

UPDATE: The problem is only present in development mode, and vanishes when running ng build or ng serve with the --prod flag.更新:该问题仅存在于开发模式中,并在使用 --prod 标志运行 ng build 或 ng serve 时消失。

I've noticed a regression in a web app I'm working on after upgrading to Angular 8. In one of my components, I've started recieving this error:在升级到 Angular 8 后,我注意到我正在处理的 Web 应用程序出现回归。在我的一个组件中,我开始收到此错误:

ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[function () { return []: 
  StaticInjectorError(Platform: core)[function () { return []: 
    NullInjectorError: No provider for function () { return [!
Error: NullInjectorError: No provider for function () { return [!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:725) [angular]
    at resolveToken (core.js:11812) [angular]
    at tryResolveToken (core.js:11756) [angular]
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:11660) [angular]
    at resolveToken (core.js:11812) [angular]
    at tryResolveToken (core.js:11756) [angular]
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:11660) [angular]
    at resolveNgModuleDep (core.js:20006) [angular]
    at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:20677) [angular]
    at getToken (router.js:2844) [angular]
    at getResolver (router.js:3492) [angular]
    at resolveNode (router.js:3475) [angular]
    at runResolve (router.js:3461) [angular]
    at MergeMapSubscriber.project (router.js:3455) [angular]
    at resolvePromise (zone.js:852) [angular]
    at resolvePromise (zone.js:809) [angular]
    at polyfills.js:3918:17 [angular]
    at Object.onInvokeTask (core.js:25977) [angular]
    at drainMicroTaskQueue (zone.js:601) [<root>]

I've narrowed the problem down to the resolver used in the route to the component.我已将问题缩小到组件路由中使用的解析器。 When I remove/comment out the injections from the constructor, the component loads fine.当我从构造函数中删除/注释掉注入时,组件加载正常。 Any ideas what could be causing this?任何想法可能导致这种情况? I'm stumped.我难住了。

Here's the resolver:这是解析器:

IMPORTS REMOVED FOR BREVITY

@Injectable()
export class MemberListResolver implements Resolve<User[]> {
  constructor(
    private authService: AuthService,
    private router: Router,
    private alertify: AlertifyService,
    private distributorService: DistributorService
  ) {
  }

  resolve(route: ActivatedRouteSnapshot): Observable<User[]> {
    return this.authService.currentDistributor.pipe(
      flatMap((d: Distributor) => {
        return this.distributorService.getUsers(d.id).pipe(
          catchError(error => {
            this.alertify.message('Problem retrieving users');
            this.router.navigate(['/home']);
            return of(null);
          })
        );
      })
    );
  }
}

I had the same problem.我有同样的问题。 It was a syntax error in the Routes array这是 Routes 数组中的语法错误

First I wrote首先我写

const routes: Routes = [
  {
    path: '',
    component: GalleryComponent,
    resolve: PhotoListResolver
  }
];

But it should be但应该是

const routes: Routes = [
  {
    path: '',
    component: GalleryComponent,
    resolve: {
        photos: PhotoListResolver
    }
  }
];

My current app is running on Angular v8.1.2我当前的应用程序在 Angular v8.1.2 上运行

Last week I had this error and I was able able to compile after adding forRoot() in a couple of files:上周我遇到了这个错误,我能够在几个文件中添加 forRoot() 后进行编译:

  1. The RouterModule in app-routing.module app-routing.module 中的 RouterModule

    import { Routes, RouterModule } from '@angular/router';
    @NgModule ({ imports: [RouterModule.forRoot(routes)], <!--edit here --> exports: [RouterModule], })

2 And if your are using NgRx, the StoreRouterConnectingModule in app.module.ts 2 如果您使用的是 NgRx,则 app.module.ts 中的 StoreRouterConnectingModule

`import { StoreRouterConnectingModule } from '@ngrx/router-store'

 @NgModule ({
   imports: Store.RouterConnectingModule.forRoot() <!--edit here -->
   })`
  1. Cancel and restart the terminal: $ ng serve取消并重启终端:$ ng serve

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 测试 - NullInjectorError: StaticInjectorError(DynamicTestModule)[NgxPermissionsDirective -&gt; NgxPermissionsService] - Angular tests - NullInjectorError: StaticInjectorError(DynamicTestModule)[NgxPermissionsDirective -> NgxPermissionsService] NullInjectorError: StaticInjectorError(Platform: core)[CompilerFactory] Angular - NullInjectorError: StaticInjectorError(Platform: core)[CompilerFactory] Angular Angular 9 - 错误:StaticInjectorError[Http]:StaticInjectorError[Http]:NullInjectorError:没有 Http 提供者 - Angular 9 - Error: StaticInjectorError[Http]: StaticInjectorError[Http]: NullInjectorError: No provider for Http 在 Angular7 生产版本中获取 StaticInjectorError 和 NullInjectorError - Getting StaticInjectorError & NullInjectorError in Angular7 production build Angular 8:错误:未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule) - Angular 8 : Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule) Angular 测试 NullInjectorError:StaticInjectorError(DynamicTestModule)[UsersEffects -> Actions]: - Angular test NullInjectorError: StaticInjectorError(DynamicTestModule)[UsersEffects -> Actions]: NullInjectorError: StaticInjectorError(DynamicTestModule) 在 Angular 2 中测试时 - NullInjectorError: StaticInjectorError(DynamicTestModule) When Testing in Angular 2 NullInjectorError: StaticInjectorError(AppModule)[AppComponent -&gt; GoogleTagManagerService]: Angular 8 - NullInjectorError: StaticInjectorError(AppModule)[AppComponent -> GoogleTagManagerService]: Angular 8 NullInjectorError: StaticInjectorError(DynamicTestModule)[ToastrService -&gt; InjectionToken ToastConfig]: 在 tslint angular 8 - NullInjectorError: StaticInjectorError(DynamicTestModule)[ToastrService -> InjectionToken ToastConfig]: in tslint angular 8 使用 primeng 进行角度测试:NullInjectorError: StaticInjectorError(DynamicTestModule)[ButtonDirective -&gt; ElementRef - Angular testing with primeng : NullInjectorError: StaticInjectorError(DynamicTestModule)[ButtonDirective -> ElementRef
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM