我尝试使用 Ngrx 效果服务,但它似乎达到了我的要求的两倍
我的派遣
this.store.dispatch(new workspacesActions.LoadWorkspaceDetailsActions(this.id));
效果服务:
export class loadDetailsWorkspaceEffectSservice {
constructor(private actions$: Actions, private WkService: WorkspacesService) {
}
@Effect() detailsWorkspace$: Observable<Action> = this.actions$
.ofType(ActionTypes.LOADWOKSPACESINFOS)
.switchMap(arg =>
this.WkService.getDetails(arg.payload))
.map((workspace) => new workspacesActions.FetchWorkspaceDetailsActions(workspace) );
}
我的服务
@Injectable()
export class WorkspacesService {
public _getDetails;
public getDetails(id: number) {
this.spinner.show()
this._getDetails = this.http.get(config.apiWorkspacesDetailsURL + id, this.jwt())
.share()
.map((response: Response) => response.json())
.catch(this.handleError)
.finally(() => this.spinner.hide());
return this._getDetails;
}
}
我的行动
export class LoadWorkspaceDetailsActions implements Action {
type = ActionTypes.LOADWOKSPACESINFOS;
constructor(public payload?: any) { }
}
export class FetchWorkspaceDetailsActions implements Action {
type = ActionTypes.FETCHWOKSPACESINFOS;
constructor(public payload?: any) { }
}
我的减速机
...
//////////////////
case WorkspacesActions.ActionTypes.LOADWOKSPACESINFOS:
return state;
//////////////////
case WorkspacesActions.ActionTypes.FETCHWOKSPACESINFOS:
const resultWS = action.payload;
return Object.assign(state, {
workspaceDetails: resultWS.workspace
});
...
我最终得到了两个 http 请求和 2 个响应!
感谢您的帮助