1.首先默认情况请求函数不能return信息到外部

错误示范

findCurrentAccountInfo() {
        let _this = this;
        let param = {};
        
        await _this.$store.dispatch("FINDCURRENTACCOUNTINFO", { param }).then(res => {
          if (res.meta.success) {
            
            return res.data
          } else {
            _this.$Message.error(res.meta.msg);
          }
        }).catch(res => {
          console.error(res);
        });
        
      }

此时执行 findCurrentAccountInfo()没有返回内容

2.再函数中声明遍历请求赋值后return

findCurrentAccountInfo() {
        let _this = this;
        let param = {};
        let resdata=''
        await _this.$store.dispatch("FINDCURRENTACCOUNTINFO", { param }).then(res => {
          if (res.meta.success) {
            
            resdata=res 
          } else {
            _this.$Message.error(res.meta.msg);
          }
        }).catch(res => {
          console.error(res);
        });
        return resdata
      }

此时执行 this.findCurrentAccountInfo()  ==> // ''  返回值为空 异步请求未来得及赋值

3.解决方法使用async….await,async声明发放为异步方法,await等待异步操作执行完毕。

async findCurrentAccountInfo() {
        let _this = this;
        let param = {};
        let resdata=''
        await _this.$store.dispatch("FINDCURRENTACCOUNTINFO", { param }).then(res => {
          if (res.meta.success) {
            
            resdata=123
          } else {
            _this.$Message.error(res.meta.msg);
          }
        }).catch(res => {
          console.error(res);
        });
        return resdata
      }

此时执行 this.findCurrentAccountInfo() 返回promise函数   比较眼熟看到光明了

4.执行promise.then() 获取返回结果

this.findCurrentAccountInfo().then(res=>{
     console.log(res)
})==》  //  123

 

 

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐