功能内部功能
我已经定义了一个使用Angular
和ngrx
13的商店加载在存储
中,以便避免重复API调用。
这是通过这种方式定义的:
shared.module.ts
/**...*/
StoreModule.forFeature(clientsFeature),
StoreModule.forFeature(prioritiesFeature),
/**...*/
client.feature.ts
import { createFeature, createSelector, createReducer, on } from '@ngrx/store';
import { ClientDTO } from '@shared/model/client.models';
import * as ClientsActions from './clients.actions';
export const initialState: ClientDTO[] = [];
export const clientsFeature = createFeature({
name: 'clients',
reducer: createReducer(
initialState,
on(ClientsActions.getClientListSuccess, (state, { clients }): ClientDTO[] => clients)
),
});
export const selectClientList = createSelector(clientsFeature.selectClientsState, clients => clients);
优先级功能相似。
我要做的是避免声明每个功能,并使用包含所有子图的“共享”功能。为此,我创建:
index.ts
import { ActionReducerMap } from '@ngrx/store';
import { ClientDTO } from '@shared/model/client.models';
import { Priority } from '@shared/model/priorities.models';
import { clientsFeature } from './clients/clients.reducer';
import { prioritiesFeature } from './priorities/priorities.reducer';
export const sharedFeatureKey = 'shared';
export interface SharedState {
clients: ClientDTO[] | null;
priorities: Priority[] | null;
}
export const reducers: ActionReducerMap<SharedState> = {
clients: clientsFeature.reducer,
priorities: prioritiesFeature.reducer,
};
和我的 sharon.module :
StoreModule.forFeature(fromShared.sharedFeatureKey, fromShared.reducers),
全部还可以。
问题
这样做我无法访问列表的内容。我确定我缺少一些东西,但我不知道。我得到了此警告:
ngrx-store.mjs:724 @ngrx/store:该州不存在功能名称“客户端”,因此CreateFeaturesElector无法访问它。确保使用Storemodule.forroot('clients',...)或Storemodule.forfeature('clients',...)在加载的模块中导入它。如果默认状态的目的是不确定的,就像路由器状态一样,则可以忽略此仅开发警告消息。
还有另一个类似的优先事项。我很确定问题出现在选择器中,但是经过几个小时之后,我找不到解决方案。
未定义是选择器内容的日志:
this.store
.select(selectPrioritiesList)
.pipe(take(1))
.subscribe(priorities => {
console.log('priorities -->', priorities);
});
提前致谢
I have defined a Store with Angular
and NgRx
13. I have a SharedModule
where I define component such as selectors, etc. Each selector content is loaded in the store
, so that I can avoid the repeat of an API call.
This is defined in this way:
shared.module.ts
/**...*/
StoreModule.forFeature(clientsFeature),
StoreModule.forFeature(prioritiesFeature),
/**...*/
clients.feature.ts
import { createFeature, createSelector, createReducer, on } from '@ngrx/store';
import { ClientDTO } from '@shared/model/client.models';
import * as ClientsActions from './clients.actions';
export const initialState: ClientDTO[] = [];
export const clientsFeature = createFeature({
name: 'clients',
reducer: createReducer(
initialState,
on(ClientsActions.getClientListSuccess, (state, { clients }): ClientDTO[] => clients)
),
});
export const selectClientList = createSelector(clientsFeature.selectClientsState, clients => clients);
Priorities feature is similar.
What I'm trying to do is avoiding to declare each feature and use a 'shared' feature that contains all the subfeatures. To do this, I create:
index.ts
import { ActionReducerMap } from '@ngrx/store';
import { ClientDTO } from '@shared/model/client.models';
import { Priority } from '@shared/model/priorities.models';
import { clientsFeature } from './clients/clients.reducer';
import { prioritiesFeature } from './priorities/priorities.reducer';
export const sharedFeatureKey = 'shared';
export interface SharedState {
clients: ClientDTO[] | null;
priorities: Priority[] | null;
}
export const reducers: ActionReducerMap<SharedState> = {
clients: clientsFeature.reducer,
priorities: prioritiesFeature.reducer,
};
And my shared.module :
StoreModule.forFeature(fromShared.sharedFeatureKey, fromShared.reducers),
All ok.
PROBLEM
Doing this I cannot access to the content of the list. I'm sure I'm missing something, but I don't know what. I get this warning:
ngrx-store.mjs:724 @ngrx/store: The feature name "clients" does not exist in the state, therefore createFeatureSelector cannot access it. Be sure it is imported in a loaded module using StoreModule.forRoot('clients', ...) or StoreModule.forFeature('clients', ...). If the default state is intended to be undefined, as is the case with router state, this development-only warning message can be ignored.
And another similar with priorities. I'm pretty sure that the problem are in the selectors, but after trying for hours, I cannot found a solution.
The undefineds are the log of the content of the selector:
this.store
.select(selectPrioritiesList)
.pipe(take(1))
.subscribe(priorities => {
console.log('priorities -->', priorities);
});
What I am doing wrong? Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于选择器。
由于您添加了一个额外的“共享”层,因此需要更新选择器。
当前,
createFeature
不可配置,并使用顶级状态选择子状态。这意味着您不能从createFeature
中使用提供的选择器,但是您必须手动编写选择器。The problem is with the selectors.
Because you added an extra "shared" layer, the selectors need to be updated.
Currently, the
createFeature
isn't configurable and uses the top level state to select sub-states. This means you can't use the provided selectors fromcreateFeature
, but you'll have to write the selectors manually.