簡體   English   中英

Angular 6-由於NGRX,生產構建失敗

[英]Angular 6 - production build failed because of NGRX

我想使用以下命令生成和測試生產版本:

ng build --prod --configuration=production 
ng serve --prod --configuration=production

這些文件已正確生成,但是當我在瀏覽器中打開網站時,出現此錯誤:

未捕獲的錯誤:StaticInjectorError [n-> t]:StaticInjectorError(平台:核心)[n-> t]:NullInjectorError:t的沒有提供者!

我的生產配置如下所示:

       "production": {
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": true,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": true,
          "buildOptimizer": true,
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
          ]
        },

我意識到問題出在我的應用程序中使用了ngrx。 當我刪除以某種方式連接到ngrx的所有內容時,該應用程序會正確打開。 您有什么想法我應該解決嗎?

下面,我在應用程序中附加了一些ngrx示例。

的AppModule

export const metaReducers: MetaReducer<AppState>[] = !environment.production ? [storeFreeze] : [];

  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    HttpClientModule,
    StoreModule.forRoot(reducers, {metaReducers}),
    EffectsModule.forRoot([AuthEffects]),
  ],

AppCompoment:

  userState: Observable<User.State>;

  constructor(private store: Store<AppState>) {}

  ngOnInit() {
    this.userState = this.store.select('user');
  }

EDITED

這是完整的AppModule:

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {AuthGuard} from './shared-module/services/auth.guard';
import {AppComponent} from './app.component';
import {HomeComponent} from './home/home.component';
import {AppRoutingModule} from './app-routing.module';
import {ErrorComponent} from './shared-module/components/error/error.component';
import {Globals} from './shared-module/services/globals';
import {UserService} from './user-module/services/user.service';
import {FacebookModule} from 'ngx-facebook';
import {AuthenticationService} from './user-module/services/authentiation.service';
import {RouteReuseStrategy} from '@angular/router';
import {CustomRouteReuseStrategy} from './router-strategy';
import {NotAllowedComponent} from './shared-module/components/not-allowed/not-allowed.component';
import {MetaReducer, Store, StoreModule} from '@ngrx/store';
import {EffectsModule} from '@ngrx/effects';
import {AppState, reducers} from './store/app.reducers';
import {AuthEffects} from './store/auth/auth.effects';
import {SharedModule} from './shared-module/shared.module';
import {UserModule} from './user-module/user.module';
import {VideoManagerService} from './video-module/services/video-manager.service';
import {HttpClientModule} from '@angular/common/http';
import {SpecialistChoiceEffect} from './store/specialist-choice/specialist-choice.effects';
import {SpecialistService} from './specialist-module/services/specialist-service';
import {environment} from '../environments/environment';
import {storeFreeze} from 'ngrx-store-freeze';

export const metaReducers: MetaReducer<AppState>[] = !environment.production ? [storeFreeze] : [];

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    NotAllowedComponent,
    ErrorComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    HttpClientModule,
    FacebookModule.forRoot(),
    AppRoutingModule,
    StoreModule.forRoot(reducers, {metaReducers}),
    EffectsModule.forRoot([AuthEffects, SpecialistChoiceEffect]),
    SharedModule,
    UserModule
  ],
  providers: [
    AuthGuard,
    Globals,
    VideoManagerService,
    UserService,
    AuthenticationService,
    SpecialistService,
    Store,
    {
      provide: RouteReuseStrategy,
      useClass: CustomRouteReuseStrategy
    }
  ],

  bootstrap: [AppComponent]
})

export class AppModule {
}

編輯2

我更改了構建配置,目前收到了更好的日志:

StaticInjectorError(AppModule)[SpecialistChoiceEffect-> MatSnackBar]:StaticInjectorError(平台:核心)[SpecialistChoiceEffect-> MatSnackBar]:NullInjectorError:MatSnackBar沒有提供者!

這是SpecialistChoiceEffect:

 constructor(private actions$: Actions,
          private router: Router,
          public infoBar: MatSnackBar) {
  }

我說,在AppModule中,我導入了SharedModule,后者導出了MaterialModule,后者又導出了MatSnackBarModule。 所以一切看起來都很好...

這似乎是CLI中的錯誤,請查看相應的Github問題

作為解決方案,您可以嘗試以下操作:

  • 更新到@ angular / cli和@ angular-devkit的最新版本(更新其他@angular軟件包可能不會受到傷害)
  • 清除您的node_modules並重新安裝

旁注:prod構建的行為有所不同,因為在開發模式下它使用AoT編譯器(提前)而不是JIT編譯器(及時)。 由於AoT模式現在幾乎和JIT一樣快,因此我建議在開發過程中也使用它,因此,如果出現問題,您將立即獲得反饋。 有關更多信息,請參見此SO 答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM