NgRx 商店 - 选择器不适用于根全局商店

NgRx store - Selectors are not working for a root global store

我是 ngrx 的新手,我只是想了解它,让一些东西发挥作用。

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

我希望有一些东西处于根状态(如果可能的话),然后我的每个功能都有单独的状态。我从根状态开始,但是 select 或者我从未收到通知。

我有以下操作...

    // 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;
    }

我创建了以下 select 或者...

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 => {` 从不触发

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

我哪里搞砸了?

更新

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

当运行,进入控制台,可以看到如下...

selector.ts:610 The feature name "appRoot" does not exist in the state, therefore createFeatureSelector cannot access it. Be sure it is imported in a loaded module using StoreModule.forRoot('appRoot', ...) or StoreModule.forFeature('appRoot', ...). If the

不明白怎么用 StoreModule.forRoot(rootReducer ),

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

如果我执行以下操作:

 StoreModule.forRoot({appRoot: rootReducer} ),

我得到一个嵌套状态:

但只是通过减速器:

StoreModule.forRoot(rootReducer ),

我没有状态:

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

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

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

我猜你的减速器配置不正确,这就是触发的动作没有改变状态的原因。由于状态 this.store.pipe(select(rootSelectors.getUrl)).subscribe(url => {}) 没有变化,因此永远不会触发此订阅。

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

有关详细信息,请参阅 docs

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

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

或者你可以这样做:

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