Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 返回承诺的Vuex操作不会解决或拒绝_Javascript_Vue.js_Promise_Axios_Vuex - Fatal编程技术网

Javascript 返回承诺的Vuex操作不会解决或拒绝

Javascript 返回承诺的Vuex操作不会解决或拒绝,javascript,vue.js,promise,axios,vuex,Javascript,Vue.js,Promise,Axios,Vuex,我正在尝试使用Vuex在VueJS应用程序中构建API服务。我正在重构一些东西以集中错误处理、清理等。我遇到的问题是通过函数调用正确地链接承诺 在根级别,我有一个BaseService类,它只是使用AXIOS(不是完整类)发出API请求: 如我的评论所示,两个控制台日志已成功完成。当我访问调用此操作的组件时,会出现此问题: this.$store .dispatch("company/COMPANY_DELETE", company.id) // Namespaced .then((re

我正在尝试使用Vuex在VueJS应用程序中构建API服务。我正在重构一些东西以集中错误处理、清理等。我遇到的问题是通过函数调用正确地链接承诺

在根级别,我有一个BaseService类,它只是使用AXIOS(不是完整类)发出API请求:

如我的评论所示,两个控制台日志已成功完成。当我访问调用此操作的组件时,会出现此问题:

this.$store
  .dispatch("company/COMPANY_DELETE", company.id) // Namespaced
  .then((response: any) => {
    console.log(response); // Never gets called
  })
  .catch((error: any) => {
    console.log(error); // Never gets called
  });

这两个控制台日志永远不会被调用。我做错了什么?

演示axios在没有额外承诺包装的情况下的操作的小示例

const store=新的Vuex.store({
声明:{
追随者:0
},
突变:{
更新跟踪程序(状态、跟踪程序){
state.followers=追随者;
}
},
行动:{
getFollowers({commit}){
返回axios.get('https://api.github.com/users/octocat)。然后((回应)=>{
提交(“updateFollowers”,response.data.followers);
返回“成功!!!”;
});
}
}
})
Vue.组件(“跟随者”{
模板:“追随者:{{computedFollowers}}”,
创建(){
这个.store.dispatch('getFollowers')。然后((结果)=>{
控制台日志(结果);
});
},
计算:{
computedFollowers(){
返回store.state.followers;
}
}
});
const app=新的Vue({
商店,
el:“#应用程序”
})

最终起作用的是取消了像赛义德所说的额外承诺。然而,在我的特定用例中,我还需要支持错误传播。因此,下面的操作包含我需要进行的修复

const action = {
  COMPANY_DELETE(context: any, id: number) {
    return companyService // Instance of CompanyService
      .delete(id)
      .then((response: any) => {
        console.log(response);
      })
      .catch((error: any) => {
        console.log(error);
        throw error; // Needed to continue propagating the error
      });
  },
};

不完全了解您的代码库是不是
“company/company\u DELETE”
正确的操作名称?如果不是
“COMPANY\u DELETE”
是的,存储模块是有名称空间的。这是正确的名字。在重构之前,我没有基本的和公司的服务,所以一切都在进行中,并且按照预期工作。调用操作的组件未更改。重构的目的是清理存储区,提高可重用性和错误处理。您的控制台中是否有错误消息?在生成过程中或控制台中没有错误消息或警告。最好在功能过程中放置几个断点,看看它在哪一点断开。这就是答案。我不需要Vuex行动中的额外承诺。然而,对于我的特定用例,我还需要支持捕获传播。我已经更新了适用于我的解决方案。谢谢你的帮助!
const action = {
  COMPANY_DELETE(context: any, id: number) {
    return new Promise((resolve, reject) => {
      companyService // Instance of CompanyService
        .delete(id)
        .then((response: any) => {
          console.log(response); // This logs successfully
          resolve(response);
        })
        .catch((error: any) => {
          console.log(error); // This logs successfully
          reject(error);
        });
    });
  }
};
this.$store
  .dispatch("company/COMPANY_DELETE", company.id) // Namespaced
  .then((response: any) => {
    console.log(response); // Never gets called
  })
  .catch((error: any) => {
    console.log(error); // Never gets called
  });
const action = {
  COMPANY_DELETE(context: any, id: number) {
    return companyService // Instance of CompanyService
      .delete(id)
      .then((response: any) => {
        console.log(response);
      })
      .catch((error: any) => {
        console.log(error);
        throw error; // Needed to continue propagating the error
      });
  },
};