gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:13:17 27 4
gpt4 key购买 nike

我是 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;

还有以下 reducer ..

    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 开发工具中

enter image description here

但是对于状态,我只看到

enter image description here

而可观察的 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} ),

我得到一个嵌套状态:

enter image description here

但只是传递 reducer :

StoreModule.forRoot(rootReducer ),

我没有状态:

enter image description here

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

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

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

最佳答案

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

参见 docs了解更多信息。

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

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

或者你可以这样做:

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

关于javascript - NgRx 商店 - 选择器不适用于根全局商店,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58263197/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com