gpt4 book ai didi

javascript - Angular 6 - 由于 NGRX,生产构建失败

转载 作者:行者123 更新时间:2023-11-30 06:23:54 25 4
gpt4 key购买 nike

我想使用此命令生成并测试生产版本:

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

文件已正确生成,但是当我在浏览器中打开网站时出现此错误:

Uncaught Error: StaticInjectorError[n -> t]: StaticInjectorError(Platform: core)[n -> t]: NullInjectorError: No provider for 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 示例。

应用模块

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

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

应用组件:

  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 {
}

EDITED 2

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

StaticInjectorError(AppModule)[SpecialistChoiceEffect -> MatSnackBar]: StaticInjectorError(Platform: core)[SpecialistChoiceEffect -> MatSnackBar]: NullInjectorError: No provider for MatSnackBar!

这是 SpecialistChoiceEffect:

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

让我说,在 AppModule 中,我导入了 SharedModule,它导出了 MaterialModule,而 MaterialModule 又导出了 MatSnackBarModule。所以一切看起来都很好......

最佳答案

这似乎是 CLI 中的一个错误,请查看相应的 Github issue .

作为解决方案,您可以尝试以下方法:

  • Update to the latest versions for @angular/cli and @angular-devkit (updating the other @angular packages probably wouldn't hurt)
  • Clear your node_modules and do a fresh install

旁注:prod 构建的行为不同,因为它在开发模式下使用 AoT 编译器(提前)而不是 JIT 编译器(及时)。由于 AoT 模式现在几乎和 JIT 一样快,我建议在开发过程中也使用它,这样如果出现问题你可以得到即时反馈。有关详细信息,请参阅此 SO answer .

关于javascript - Angular 6 - 由于 NGRX,生产构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51627461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com