angular - @ngrx/store 不适用于 ng serve -prod
问题描述
NgRx 在开发模式下服务时运行良好ng serve
,但在 prod 模式下服务ng serve -prod
或构建时, ng build --prod
ngrx 不工作更多。
解决方案
根据 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 编译引擎正确识别您尝试在构建时定义的内容。
推荐阅读
- python-3.x - 没有名为 mysite.wsgi 的模块 django Heroku 部署
- plot - 将情节与不同的传说相结合
- c++ - 是否可以在这里减少模板参数?
- reactjs - 如果未通过身份验证,则将重定向重定向到登录-Spring 安全性
- javascript - Javascript url 代码不会触发 Wordpress 中的引导模式
- sql - 如何管理 Facebook 等用户通知设置?
- typescript - 如何让 VSCode 使用 Yarn 识别我的 TypeScript 导入?
- reporting-services - 对 SSRS 中的一列表达式求和(总和)
- python - 为什么 [1: : -1] 会返回列表的前两项但将其反转而不是将第二项返回到列表的末尾?
- sql - 将正值应用于后续行,直到该值再次变为负值