Angular生产构建运行时NGRX Store报'release'未定义类型错误
解决NGRX生产构建中"TypeError: Cannot read property 'release' of undefined"的问题
我一眼就注意到你代码里的关键问题——在account.selectors.ts中错误地使用了this关键字,这就是导致生产构建报错的根源。
问题分析
在模块级别的selector定义中,this并不指向当前模块的上下文(严格模式下甚至是undefined)。开发环境可能因为宽松的模式没有触发报错,但生产构建时代码会被压缩优化,this的指向问题会直接暴露,进而引发NGRX内部处理selector时的连锁错误,最终出现你看到的"Cannot read property 'release' of undefined"。
修复方案
直接移除selector定义里的this,直接引用你已经定义好的getCompleteAccountState即可:
修改后的account.selectors.ts代码:
import { createSelector } from '@ngrx/store'; import * as fromReducer from '../reducers/account.reducers'; import * as fromFeature from '../reducers'; export const getCompleteAccountState = createSelector( fromFeature.getAccountState, (state: fromFeature.AccountState) => state.account ); export const getAccountLoading = createSelector( getCompleteAccountState, // 移除this fromReducer.getAccountLoading ); export const getAccountLoaded = createSelector( getCompleteAccountState, // 移除this fromReducer.getAccountLoaded ); export const getAllAccountItems = createSelector( getCompleteAccountState, // 移除this fromReducer.getAccountItems );
额外验证点
- 确认
StoreModule.forFeature('account', reducers)中的reducers确实对应AccountState的结构,确保feature模块的store注册正确。 - 检查
AccountEffects中是否有异步操作的错误处理,避免因为数据加载异常间接触发类似问题。
这个修改应该能解决你生产构建中的报错问题,因为它修复了selector引用的根本错误,让NGRX能够正确处理状态选择和订阅的生命周期。
内容的提问来源于stack exchange,提问作者FabianGosebrink
相关产品推荐
相关产品推荐

