angular - 将 3 个相同类型的对象保存在一个特征存储中,并且完全分离。
问题描述
我有三个服务,它们根据不同的操作返回相同类型的对象。我怎样才能以干净的分离来保存特征存储中的所有对象。
行动 1:LOAD_CANDIDATES
I have an effect that invokes below service call
public getCandidates(): Observable {
const url = 'Candidates Url';
return this.http.get(url);
}
行动 2:LOAD_MATCHED_CANDIDATES
I have an effect that invokes below service call
public getMatchingCandidates(conditions: any): Observable
{
const url = 'Matched Candidates Url';
return this.http.get(url);
}
行动 3:LOAD_ASSIGNED_CANDIDATES
I have an effect that invokes below service call
public getAssignedCandidates(id: number): Observable {
const url = 'Assigned candidates url';
return this.http.get(url);
}
我确实对他们每个人都有成功和失败的影响。
Candidate reducer :
export const reducers = {
search: fromSearch.reducer,
candidates: fromCandidates.reducer,
collection: fromCollection.reducer
};
Here is the injection of feature store to module
StoreModule.forFeature('candidates', combineReducers(fromStore.reducers))
我怎样才能在特征存储中同时拥有所有匹配和分配的候选对象,并通过明确的分离来指定相应的对象类型(即这些是匹配的,这些是分配的)
解决方案
为了实现这一点,您需要使用多个属性对相应的状态进行切片,并在 reducer 中进行处理。请参阅下面的代码以获取示例实现
将“候选人”声明为“CandidateState”类型
export interface CandidateState {
allcandidates: Array<Object>;
matchedcandidates: Array<Object>;
assignedcandidates: Array<Object>;
}
现在组成你的候选减速器如下
export const candidateInitialState: fromState.CandidateState = {
allcandidates: [],
matchedcandidates: [],
assignedcandidates: []
};
export const reducer = (
state: fromState.CandidateState = candidateInitialState,
action: fromAction.CandidateActions
): fromUserState.CandidateState => {
switch (action.type) {
case fromAction.LOAD_CANDIDATES: {
return {
...state,
allcandidates: action.allCandidatesData
};
}
case fromAction.LOAD_MATCHED_CANDIDATES: {
return {
...state,
matchedcandidates: action.matchedCandidatesData
};
}
case fromAction.LOAD_ASSIGNED_CANDIDATES: {
return {
...state,
assignedcandidates: action.assignedCandidatesData
};
}
default:
return state;
}
};
推荐阅读
- dart - flutter 获取布局渲染事件
- unicode - 为什么 NFKC 规范化会丢失上标和下标信息?
- c++ - 为什么基指针可以访问虚函数中的派生成员变量
- sql - 根据最新日期和 SQL Server 2008 R2 中的不同列获取表的最新行
- c# - 在没有静态引用的静态方法中修改另一个类中的类属性
- javascript - 使用第二个 jquery 请求上传文件
- amazon-web-services - 将 robots.txt 文件放入 S3 存储桶
- python - 基于多个键或多个键组合或合并字典
- javascript - Heroku Rails JS管道问题
- java - 连接到 HTML 客户端的 Java 服务器套接字