首页 > 解决方案 > @ngrx/store 不适用于 ng serve -prod

问题描述

NgRx 在开发模式下服务时运行良好ng serve,但在 prod 模式下服务ng serve -prod或构建时, ng build --prodngrx 不工作更多。

示例应用程序仓库

标签: angularngrxngrx-store

解决方案


根据 NGRX 状态的文档。这是处理您看到的 AOT 构建问题的正确格式。

“以前为了兼容 AOT,需要将一个函数传递给 provideStore 方法,以将减速器组合成一个根减速器。initialState 也作为第二个参数中的对象提供给该方法。”

“这已被简化为只需要一个由库组成的 reducer 映射。第二个参数是一个配置对象,您可以在其中提供初始状态。”

他们的示例如下所示 reducer index.ts

import { ActionReducerMap } from '@ngrx/store';

export interface State {
  auth: fromAuth.State;
  layout: fromLayout.State;
}

export const reducers: ActionReducerMap<State> = {
  auth: fromAuth.reducer,
  layout: fromLayout.reducer,
};

在你的app.module

import { StoreModule } from '@ngrx/store';
import { reducers } from './reducers';

@NgModule({
  imports: [
    StoreModule.forRoot(reducers, {
      initialState: {
        auth: {
          loggedIn: true,
        },
      },
    }),
  ],
})
export class AppModule {}

我认为您的代码缺少初始状态声明,该声明允许 AOT 编译引擎正确识别您尝试在构建时定义的内容。


推荐阅读