找回密码
 立即注册

QQ登录

只需一步,快速开始

ctjl_yf

注册会员

17

主题

65

帖子

192

积分

注册会员

积分
192

微信认证勋章

ctjl_yf
注册会员   /  发表于:2021-7-19 11:11  /   查看:1977  /  回复:5
1、前端集成框架:VUE。
2、demo示例如下:
  1. test() {
  2.      let json = this.spread.toJSON();
  3.      alert(1);

  4.      this.excelIo.save(json, (blob) => {
  5.          alert(2)
  6.      })

  7.      alert(3);
  8. },
复制代码
3、当前alert结果:1  3  2。
4、需求:alert结果为  1 2 3。
5、备注:已尝试过如下代码,未生效。

  1. async test() {
  2.      let json = this.spread.toJSON();
  3.      alert(1);

  4.     await this.excelIo.save(json, (blob) => {
  5.          alert(2)
  6.     })

  7.      alert(3);
  8. },
复制代码




5 个回复

倒序浏览
Clark.Pan讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2021-7-19 11:34:36
沙发
excelIo.save本身就是异步操作,如需同步处理,可以在操作的successcallback中去处理,例如:
  • test() {
  •      let json = this.spread.toJSON();
  •      alert(1);
  •      this.excelIo.save(json, (blob) => {
  •          alert(2);
  •          alert(3);
  •      })
  • }

回复 使用道具 举报
ctjl_yf
注册会员   /  发表于:2021-7-19 11:38:32
板凳
Clark.Pan 发表于 2021-7-19 11:34
excelIo.save本身就是异步操作,如需同步处理,可以在操作的successcallback中去处理,例如:
  • test() { ...

  • 有其他设置为同步的方案吗?因为我这边还涉及到 test() 这个方法的返回值。如下:

    1. save() {
    2.      let result = this.test();
    3.      // 对result的一系列处理
    4. },


    5. test() {
    6.      let result = ""
    7.    
    8.      this.excelIo.save(json, (blob) => {
    9.          // 一系列处理后对result赋值
    10.          result = "xxxxx"
    11.      })

    12.      return result;
    13. }
    复制代码



    回复 使用道具 举报
    Clark.Pan讲师达人认证 悬赏达人认证 SpreadJS 开发认证
    超级版主   /  发表于:2021-7-19 11:43:01
    地板
    那就外层不要返回,在excelIo.save中返回就好了,本身异步处理您的那种方法就是有问题的。正确的方式就是上面我跟您说的那种
    回复 使用道具 举报
    ctjl_yf
    注册会员   /  发表于:2021-7-19 15:49:13
    5#
    Clark.Pan 发表于 2021-7-19 11:43
    那就外层不要返回,在excelIo.save中返回就好了,本身异步处理您的那种方法就是有问题的。正确的方式就是上 ...

    在内层返回的话没法满足现有需求得哇。如下:
    1. // A服务保存
    2. save() {
    3.     let result = this.upload();
    4.    
    5.     // 构造参数,调用A服务接口
    6.     let param = {
    7.           "xx": result,
    8.           ...
    9.     }
    10.    
    11.   
    12.    axios({....})

    13. }

    14. // 将文件上传至B服务,获得某返回值
    15. upload() {
    16.      let result = null;
    17.      let json = this.spread.toJSON();

    18.      this.excelIo.save(json, (blob) => {      
    19.          // 构造form表单,且调用B服务后台接口,拿到返回值
    20.         let form = new FormData(...)
    21.         form.append(...)

    22.         axios({ ... }).then(res => {
    23.             // 一系列处理后,赋值result
    24.             result = res.data
    25.         })
    26.      })
    27.    
    28.     return result;
    29. }
    复制代码


    是否有方案使得excelIo.save方法同步执行呢?



    回复 使用道具 举报
    Clark.Pan讲师达人认证 悬赏达人认证 SpreadJS 开发认证
    超级版主   /  发表于:2021-7-19 18:33:25
    6#
    本帖最后由 Clark.Pan 于 2021-7-19 18:34 编辑

    SpreadJS没有excelIo.save是异步执行,后续估计也不会提供。如果您需要上传之后保存,可以在
    axios({ ... }).then(res => {
    里面嵌入调用upload
    }
    这是异步执行的一种方式,另外可以尝试用promise去做,相关资料我替您百度了一下,供您参考。https://www.jianshu.com/p/814704996323

    https://www.cnblogs.com/ccylovehs/p/10425199.html
    回复 使用道具 举报
    您需要登录后才可以回帖 登录 | 立即注册
    返回顶部