如何消除"The feature name "router“状态不存在...”ngrx/router-store警告

How to silence "The feature name "router" does not exist in the state..." ngrx/router-store warning

当我将“@ngrx/router-store”添加到我的项目时,它会在开发模式和单元测试结果中向应用程序控制台发送垃圾邮件,并显示以下消息:

The feature name "router" does not exist in the state, therefore createFeatureSelector cannot access it. Be sure it is imported in a loaded module using StoreModule.forRoot('router', ...) or StoreModule.forFeature('router', ...). If the default state is intended to be undefined, as is the case with router state, this development-only warning message can be ignored.

我尝试将 router 状态 属性 类型设置为

router: null |  RouterReducerState<SerializedRouterStateSnapshot>

和初始状态值

router: null

但它与 routerReducer 类型冲突,后者只接受

RouterReducerState<SerializedRouterStateSnapshot>

如何禁用此警告?老实说,我觉得这很烦人。

这对我有帮助:不要使用 createFeatureSelector 创建路由器功能选择器,而是使用 createSelector

export const routerState = createSelector((state: State) => state.router, value => value)

出现此问题是因为 createFeatureSelector logs a warning 如果特征值等于 undefined。上面的代码等同于原来的实现,但是没有日志。

更新: here's a PR 旨在解决问题。

尝试select具有简单功能的路由器状态切片

export const selectRouter = (state: State) => state.router;

因为 createFeatureSelector(因此得名)用于select添加到 FEATURE 模块中的状态片段

StoreModule.forFeature('feature1', reducers)

在这种情况下,您应该使用 createFeatureSelector

export const selectFeature1 = createFeatureSelector<State, Feature1State>('feature1');

你的 switch 语句中有 default return 吗?

 default:
   return state;