NGRX如何在HTTP呼叫生效之前派遣操作
我是NGRX /反应性模式的新手。我正在尝试重新设计我们从模板驱动的方法(使用组件驱动模型)中具有的一些复杂形式,以使用NGRX进行反应性形式。
由于我们的API的编写方式,在将请求发布到服务器之前,需要操纵表单中的某些数据。 在我的旧设计中,这是一个有些复杂的功能,我必须用大量条件编写,但是由于所有内容都直接与组件本身绑定,所以这是进行数据操作的直接方法,然后
立即 订阅端点我试图将NGRX和NGX Formally用于表单,因此我不必具有多个组件和一吨混乱的代码,但是我仍然需要在数据登录之前操纵数据,端点
有办法NGRX效果对我来说是在HTTP调用之前派遣动作的?
我仍在学习ngrx,所以只知道基本呼叫,但基本上,需要知道如何使表单提交派遣措施首先调用还原行动以更改状态,然后在状态已更新,调用效果将数据发送到端点,
这是我试图创建的还原函数,
export const submitToRmv = (state: RmvServicesState, action) => {
let vehicleArray = [...state.rmvData.rmvServicesUserEnteredData.vehicles];
const transactionType = state.rmvData.transactionType;
if (transactionType === 'PrefillRegistrationTransfer' || vehicleArray[0].reassignedPlate === 'Y') {
const newVeh = {
// modified codehere ...
};
vehicleArray.push(newVeh);
vehicleArray[0].plateNumber = '';
vehicleArray[0].plateType = '';
}
vehicleArray = [...vehicleArray, ...state.rmvData.rmvServicesUserEnteredData.tradeInVehicles];
const newTransactionType = transactionType === 'PrefillNewTitleAndRegistration' || transactionType === 'PrefillRegistrationTransfer' &&
vehicleArray[0].reassignedPlate === 'Y' ? 'Reassign' : transactionType.replace('Prefill', '');
return {
...state,
rmvData: {
...state.rmvData,
transactionType: newTransactionType,
rmvServicesUserEnteredData: {
...state.rmvData.rmvServicesUserEnteredData,
vehicles: vehicleArray,
},
},
};
};
我创建了一组操作
export const validationSubmission = createAction(
'[Rmv Prefill Api] Rmv Validation Submission',
props<{getReadyRequest: RmvServicesData }>()
);
export const validationResponse = createAction(
'[Rmv Prefill Api] Rmv Validation Response',
props<{response: RmvValidationResponse }>()
);
我的效果是以我的形式触发该操作
rmvSubmit$ = createEffect(() => {
return this.actions$.pipe(
ofType(RmvPrefillActions.validationSubmission),
mergeMap((value) =>
this.rmvService.getReadySubmit(value.getReadyRequest)
.pipe(map(res => RmvPrefillActions.validationResponse({response: res}))
)
));
});
,我要派遣该动作,我希望发生的事情是使还原器函数执行代码以操纵数据,然后以某种方式(一旦完成),然后触发该代码影响 在我的测试中,看起来效果首先是触发的,因为发布到我的API的值是未修改的数据,然后当我查看Redux Dev工具时,我确实看到我的值已经改变,因此Reducer正在工作,只是我不想顺序
I am new to ngrx / reactive pattern. I am trying to redesign some complex forms that we have from a template driven approach with component driven models to reactive forms using ngrx.
due to the way our api is written, some of the data from the form needs to be manipulated before posting the request to the server.
In my old design, this was a somewhat complex function that I had to write with a ton of conditionals, but because everything was tied directly to the component itself, it was a straightforward approach to do the data manipulation, then subscribe to the endpoint
Now I am trying to use ngrx and ngx-formly for the form, so that I don't have to have multiple components and a ton of cluttered code, but I still need to manipulate the data before it hits the endpoint
Is there a way in ngrx effects for me to dispatch an action right before the http call?
I am still learning ngrx kind of on the fly, so only know the basic calls, but basically, need to know how I can make the form submission dispatch an action that would first call a reducer action to change the state, then right after the state has been updated, call an effect to send that data to the endpoint
Here is the reducer function I am trying to create
export const submitToRmv = (state: RmvServicesState, action) => {
let vehicleArray = [...state.rmvData.rmvServicesUserEnteredData.vehicles];
const transactionType = state.rmvData.transactionType;
if (transactionType === 'PrefillRegistrationTransfer' || vehicleArray[0].reassignedPlate === 'Y') {
const newVeh = {
// modified codehere ...
};
vehicleArray.push(newVeh);
vehicleArray[0].plateNumber = '';
vehicleArray[0].plateType = '';
}
vehicleArray = [...vehicleArray, ...state.rmvData.rmvServicesUserEnteredData.tradeInVehicles];
const newTransactionType = transactionType === 'PrefillNewTitleAndRegistration' || transactionType === 'PrefillRegistrationTransfer' &&
vehicleArray[0].reassignedPlate === 'Y' ? 'Reassign' : transactionType.replace('Prefill', '');
return {
...state,
rmvData: {
...state.rmvData,
transactionType: newTransactionType,
rmvServicesUserEnteredData: {
...state.rmvData.rmvServicesUserEnteredData,
vehicles: vehicleArray,
},
},
};
};
I created a set of actions
export const validationSubmission = createAction(
'[Rmv Prefill Api] Rmv Validation Submission',
props<{getReadyRequest: RmvServicesData }>()
);
export const validationResponse = createAction(
'[Rmv Prefill Api] Rmv Validation Response',
props<{response: RmvValidationResponse }>()
);
my effect is to trigger that action
rmvSubmit$ = createEffect(() => {
return this.actions$.pipe(
ofType(RmvPrefillActions.validationSubmission),
mergeMap((value) =>
this.rmvService.getReadySubmit(value.getReadyRequest)
.pipe(map(res => RmvPrefillActions.validationResponse({response: res}))
)
));
});
In my form, I am dispatching that action, and what I was hoping to happen was get the reducer function to execute the code to manipulate the data, then somehow, once it is finished, to then trigger the effect
In my testing, it looks like the effect is triggering first, as the values being posted to my api is the unmodified data, then when I look at the redux dev tools, I do see that my values have changed, so reducer is working, just not in the order I want it to
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您需要执行单独的操作:
这是样式,我喜欢在:
现在您创建一个效果来应对“ Dosomething”动作的成功,当动作发射并发射操作时,还原器将对应效果对它们做出反应。如果您可以创建一个stackblitz,我很乐意为您编辑代码,以便在此结构中使用。
yes, you need to do separate actions:
This is the style, I like to write my actions in:
Now you create an effect to deal with the success of the 'doSomething' action, the reducers will correspond when the actions are fired and the effects react to them. If you can create a stackblitz, I am happy to edit the code for you so it can work in this structure.