0

我有一个 Angular 6 + ngredux 的项目,我想为下一种情况提供建议:

  • 我有一个概述详细信息页面(只读信息),其中填充了来自应用程序状态的数据。在这个页面中,我有一些按钮可以打开其他页面(这里我们有一些表单——在这些表单中,用户可以编辑一些字段)。
  • 当用户保存数据时,他应该返回到概览详情页面。由于我们使用的是 ngredux 并且我们从应用程序状态更新值,因此我们在组件中使用如下组件处理此问题:

    onSubmit(){
    this.actionsCreator.save(this.model);
    const route = 'details/' + this.objId;
    this.router.navigate([route]);
    } 
    

我们遇到的问题是,如果 API 中的“save”方法耗时 2-3 秒,则先重定向用户,然后在 app 状态下保存并更新模型(这很正常 - 我们这样做以错误的方式重定向)。

为了分派我们使用自定义中间件的操作。

return next => action => {
const {type, callApi, shouldCallApi, success = function () { }} = action;
if (!shouldCallApi) { return next(action);}
if (typeof callApi !== 'function') {throw new Error('Expected callApi to be a function');}
if (typeof shouldCallApi !== 'function') {throw new Error('Expected shouldCallApi to be a function');}
if (!shouldCallApi(getState())) {return;}

return callApi().subscribe(response => {
    success(response);
    return dispatch({type: type,payload: response || null});
    });
};

因此,我想要一个关于如何处理这种情况的建议。我们是否应该在 actionsCreator 服务中注入路由器(这是正确的方法)并在调度时进行重定向(在成功的情况下)?还是来自减速机?还是仅来自组件?

4

0 回答 0