【发布时间】:2018-08-01 07:17:34
【问题描述】:
我想使用以下命令生成和测试生产版本:
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的例子。
应用模块
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');
}
已编辑
这是一个完整的 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。所以一切看起来都很好......
【问题讨论】:
-
这个错误的意思是你没有在你的 app.module.ts 中提供你的服务之一,你能发布你的整个 app.module.ts
-
好的,我添加了这个
-
@SmokeyDawson 你有什么提示如何识别丢失的服务吗?
-
您是否正在运行测试/应用中是否有 .spec 文件?如果是这样,您需要确保您也在组件 TestBed.configureTestingModule({ .. }) 中导入 MaterialModule。
-
是的,测试通过了
标签: javascript angular angular-material angular-cli ngrx