首页 > 解决方案 > NgRx 存储 - 选择器不适用于根全局存储

问题描述

我对 很陌生ngrx,只是想弄清楚它,并得到一些工作。

我已将 ngrx(8.3 版)添加到我的应用程序中。

我希望有一些东西处于根状态(如果可能的话),然后对我的每个功能都有单独的状态。我从根状态开始,但我拥有的选择器从未得到通知。

我有以下动作...

    // actions
    import { createAction, union } from '@ngrx/store';
    import { SecurityTokensState } from './app.reducer';

    export const setUrl = createAction(
      '[App url] ',
      (payload: string) => ({ payload })
    );

    export const setTokens = createAction(
      '[App setSecurityTokens] ',
      (payload: SecurityTokensState) => ({ payload })
    );


    export const actions = union({
      setUrl,
      setTokens
    });

    export type ActionsUnion = typeof actions;

以及以下减速机..

    import * as rootActions from './app.actions';
    import { createReducer, on } from '@ngrx/store';

    /** Top level state */
    export interface State {
      /** State to do with Auth */
      tokens: SecurityTokensState;

      /** General / root app state (eg configuration) */
      app: AppState
    }

    /** App wide general state */
    export interface AppState {
      url: string;
      //extraLogging: boolean;
      //offlineExpiry: number; 
      //offlineTime: number;
    }

    /** Security token state */
    export interface SecurityTokensState {
      token: string,
      refreshToken: string;
    }

    const initialState: State = { tokens: undefined, app: { url: ""}  };

    export function rootReducer(state: State, action: rootActions.ActionsUnion): State {
      return reducer(state, action);
    }

    const reducer = createReducer(
      initialState,
      on(rootActions.setTokens,
        (state, { payload }) => ({ ...state, tokens: payload })
      ),
      on(rootActions.setUrl,
        (state, { payload }) => ({ ...state, app: updateUrl(state, payload)}))
    )

    /**  Helper to update the nested url */
    const updateUrl = (state: State, payload: string): AppState => {      
      const updatedApp = { ...state.app };
      updatedApp.url = payload;
      return updatedApp;
    }

我创建了以下选择器...

import { createFeatureSelector, createSelector } from "@ngrx/store";
import { AppState } from './app.reducer';

const getAppState = createFeatureSelector<AppState>('app');

export const getUrl = createSelector(
  getAppState,
  state => state.url
  );

在 app.module 中,我有以下...

StoreModule.forRoot(rootReducer),

现在,在一个组件中,我有

   import * as rootSelectors from '../state/app.selectors';
    ....

    public onUrlBlur(ev : any): void {   
       let val = ev.target.value;
       this.store.dispatch(rootActions.setUrl(val));   
      }

我有订阅更新的代码

 this.subs.sink = 
    this.store.pipe(select(rootSelectors.getUrl)).subscribe(url => {
    this.urlEntered = url        
  });

最后,作为占位符,在我的一个功能模块中,我添加了...

   StoreModule.forFeature('myfeature1', {})

我看到模糊函数被调用,并且在我看到的 redux 开发工具中

在此处输入图像描述

但对于国家来说,我所看到的只是

在此处输入图像描述

而可观察的 this.store.pipe(select(rootSelectors.getUrl)).subscribe(url => {` 永远不会触发

所以我的根状态似乎并不在那里,我真的看不出我做错了什么。

我在哪里搞砸了?

更新

在这里添加了一个非常相似的例子(有同样的问题)

运行时,转到控制台,可以看到以下...

selector.ts:610 状态中不存在功能名称“appRoot”,因此 createFeatureSelector 无法访问它。确保使用 StoreModule.forRoot('appRoot', ...) 或 StoreModule.forFeature('appRoot', ...) 将其导入加载的模块中。如果

我不明白如何使用StoreModule.forRoot(rootReducer ),

在错误中,它建议使用字符串...例如StoreModule.forRoot('app', rootReducer ),,但这会产生语法错误。

如果我执行以下操作:

 StoreModule.forRoot({appRoot: rootReducer} ),

我得到一个嵌套状态:

在此处输入图像描述

但只是通过减速器:

StoreModule.forRoot(rootReducer ),

我没有状态:

在此处输入图像描述

我看到的所有示例都只使用了功能状态,但我有一些设置只是应用程序范围内的,而不是在功能模块中。

此外,由于此状态不在功能模块中,我不确定是否应该使用 createFeatureSelector:

const getAppState = createFeatureSelector<AppState>('appRoot');

标签: javascriptangulartypescriptngrxngrx-store

解决方案


StoreModule.forRoot()函数需要一个ActionReducerMap,而不是 reducer 函数。

有关更多信息,请参阅文档

要解决嵌套状态问题,在您的情况下,这将如下所示:

StoreModule.forRoot({
  tokens: tokensReducer,
  appRoot: appRootReducer
})

或者你可以这样做:

StoreModule.forRoot({}),
StoreModule.forFeate('appRoot', appRootReducer)

推荐阅读