zayzy 2022-09-07 09:29 采纳率: 94.1%
浏览 38
已结题

关于promise的问题

img

在vuex中这样写promise对吗?为什么获得的结果是undefined?

我认为.then应该写在上图倒数第二行蓝括号后边。

  • 写回答

5条回答 默认 最新

  • Z_pigeon 2022-09-07 09:38
    关注

    是对的哦,首先输出下你这里response的值,看看接口到底有没值先,如果没值那这里就是问题了,那你就要看看你的l请求有没成功,login函数为何没返回值;如果说有值,那么就是你的调用问题了,调用是类似如此吗,this.$store可以不用管,可能你是store或者其他,这个没关系值对就行。关键是是否使用dispatch

    this.$store.dispatch('user/login', loginInfo).then((res) => {
        console.log('这里拿到res数据,也就是你vuex定义里面的response')
    })
    
    或者是async函数中使用await
    let res = await this.$store.dispatch('user/login', loginInfo)
    console.log('这个时候也能拿到值')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 11月2日
  • 已采纳回答 10月25日
  • 创建了问题 9月7日