try-catch

  1. const actions = {
  2. async addOrUpdateShopCart({ commit }) {
  3. //注意:async函数执行返回的结果一定是一个promise【要么成功,要么失败】
  4. let result = await reqAddOrUpdateShopCart();
  5. if (result.code == 200) {
  6. //返回的是成功的标记
  7. return "ok";
  8. } else {
  9. //返回的是失败的标记
  10. return Promise.reject(new Error("faile"));
  11. }
  12. },
  13. };
  14. //组件使用
  15. methods:{
  16. async addShopcar(){
  17. try{
  18. await this.$store.dispatch('addOrUpdateShopCart');
  19. }catch(error){
  20. alert(error.message);
  21. }
  22. }
  23. },

Promise.all多个接口一起调用

image.png
image.png