代码之家  ›  专栏  ›  技术社区  ›  Ljiljana Matic

NullInjectorError:没有函数{}的提供程序!

  •  0
  • Ljiljana Matic  · 技术社区  · 7 年前

    这是我在Angular7项目中设置自定义AOT的几个小时后的最后一个选择,我得到了

    Uncaught Error: StaticInjectorError(Platform: core)[function(){}]: 
      NullInjectorError: No provider for function(){}!
        at t.get (injector.ts:43)
        at injector.ts:346
        at Xa (injector.ts:288)
        at t.get (injector.ts:168)
        at Cp (application_ref.ts:41)
        at t.bootstrapModule (application_ref.ts:262)
        at Object.<anonymous> (main-aot.ts:14)
        at n (bootstrap:19)
        at bootstrap:83
        at bootstrap:83
    

    这是我的应用模块.ts

    import { AppComponent } from './app.component';
    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { HttpClient, HttpClientModule } from '@angular/common/http';
    import { InlineSVGModule } from 'ng-inline-svg';
    import { APP_BASE_HREF } from '@angular/common';
    import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
    import { FeatureRoutingModule } from './app-routing.module';
    /**
     * Core
     */
    import { HeaderComponent } from './core/header/header.component';
    import { AppSettingsComponent } from './core/header/app-settings/app-settings.component';
    import { SidebarComponent } from './core/sidebar/sidebar.component';
    import { PreloaderComponent } from './core/preloader/preloader.component';
    import { TranslationLoader } from './services/translation.service';
    /**
     * Shared
     */
    import { PopupsDirective } from './shared/popups/popups.directive';
    /**
     * Component
     */
    import { StructureComponent } from './component/structure/structure.component';
    import { OverviewComponent } from './component/overview/overview.component';
    import { MainContentComponent } from './component/main-content/main-content.component';
    import { NavigationStripeComponent } from './component/main-content/navigation-stripe/navigation-stripe.component';
    import { TestDirective } from './component/tooltip/serviceCalls.directive';
    import { TooltipService } from './component/tooltip/tooltip.service';
    import { TooltipComponent } from './component/tooltip/tooltip.component';
    
    export function HttpLoaderFactory(http: HttpClient) {
      return new TranslationLoader(http);
    }
    
    @NgModule({
      // rendering in browser environment
      imports: [
        BrowserModule,
        FeatureRoutingModule,
        TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useFactory: (HttpLoaderFactory),
            deps: [HttpClient]
          }
        }),
        InlineSVGModule.forRoot(),
        HttpClientModule
      ],
      // register services at root level
      providers: [
        TooltipService,
        { provide: APP_BASE_HREF, useValue: `${window['_app_base']}/` || '/' }
      ],
      // components declarations
      declarations: [
        AppComponent,
        HeaderComponent,
        AppSettingsComponent,
        SidebarComponent,
        PreloaderComponent,
        StructureComponent,
        OverviewComponent,
        MainContentComponent,
        TestDirective,
        TooltipComponent,
        PreloaderComponent,
        PopupsDirective,
        NavigationStripeComponent
      ],
      // bootstrap component
      bootstrap: [AppComponent]
    })
    export class AppModule {
    }
    

    这是我的组件

    import { Component, OnInit } from '@angular/core';
    import { AppService } from './services/app.service';
    import { TranslateService } from '@ngx-translate/core';
    
    @Component({
      selector: 'kpi-main',
      templateUrl: './app.component.html'
    })
    export class AppComponent implements OnInit {
      theme: string;
      sidebarPref: object = {};
      stickyHeader: string = '';
      user: object = {};
    
      constructor (private appService: AppService, private translate: TranslateService) {
        // this language will be used as a fallback when a translation isn't found in the current language
        translate.setDefaultLang('en');
         // the lang to use, if the lang isn't available, it will use the current loader to get them
        translate.use('en');
      }
    
      ngOnInit(): void {
        this.appService.getLayoutPreferences().subscribe(layout => {
          this.theme = layout.theme.value;
          this.sidebarPref = layout.sidebar_small;
          this.stickyHeader = layout.stickyHeader.visible;
          this.user = layout.sidebar_small.user;
        });
      }
    }
    

    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { Response } from '@angular/http';
    import { Observable } from 'rxjs';
    
    @Injectable({
      providedIn: 'root'
    })
    export class AppService {
      theme: string = '';
    
      constructor (private http: HttpClient) { }
    
      setSidebarPref(obj: object): Observable<any> {
        return this.http.put('../api/v1.0/users/me/preferences', { 'batch' : obj }, { withCredentials: true } );
      }
    
      getUserImage(): Observable<any> {
        return this.http.get('../api/v1.0/users/me/image', { responseType: 'blob', withCredentials: true });
      }
    
      getLayoutPreferences(): Observable<any> {
        return this.http.get('../api/v1.0/properties/layout', { withCredentials: true });
      }
    
      handleError(error: Response) {
        console.log(error);
        return Observable.throw(error.json().error || 'Server error');
      }
    }
    

    我的应用程序上的代码我得到了空注入器异常,你在我问题顶部的链接中看到了这个异常。只有在生产模式下运行时才会抛出此错误(我已经禁用了UglifyJsPlugin以获得更好的错误消息,但仍不清楚是什么错误)

    附笔:如果需要其他文件检查,请告诉我。

    更新 经过一些搜索,我发现这个问题出现在main-aot.ts文件中,以及ngfactory的创建方式。这是我的新起点,这是文件

    import 'core-js/es7/reflect';
    import 'zone.js/dist/zone';
    
    import { platformBrowser } from '@angular/platform-browser';
    import { AppModule } from './app/app.module';
    
    /**
     * Enable production mode. Disable Angular's development mode, which turns off assertions and other
     * checks within the framework.
     */
    import { enableProdMode } from '@angular/core';
    
    // enable production mode and thus disable debugging information
    enableProdMode();
    
    platformBrowser().bootstrapModule(<any>AppModule)
      .catch(err => console.error(err));
    0 回复  |  直到 7 年前
        1
  •  1
  •   Edric Prince Bansal    7 年前

    在你的 TranslateModule import,传递的是括号(可能是函数),而不是 loader.useFactory :

    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        // useFactory: (HttpLoaderFactory), <- Here
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      }
    }),
    // ...
    
        2
  •  0
  •   Ljiljana Matic    7 年前

    如果有人面临同样的问题,可以在这里找到答案。尽管报道的问题不是我所经历过的,但ckapilla的回答解决了我的问题。

    @ngtools\webpack AOT doesn't work OR freezes at 95% emitting

    推荐文章