• VUE- 异步等待方法嵌套


    VUE- 异步等待方法嵌套

    vue在一个方法执行完后执行另一个方法
    用Promise来实现。
    Promise是ES6的新特性,用于处理异步操作逻辑,用过给Promise添加then和catch函数,处理成功和失败的情况

    ES7中新提出async搭配await,建议使用async搭配await。
    使用方法:async/await使用方法 

    1. Promise的使用方法

    //定义方法 
     testFunc()
      { 
          return new Promise((resolve, reject) => {
            this.$axios.get('http://localhost/MKCurtain/logsService/HLogs.ashx?function=GetFilesByPath&filePath=D:/WebSite/MKCurtainJoin/logs',{})
              .then(res => {resolve(res.data);})
              .catch(function(error) {reject(error.message);});
          });
      }
     
     
    //方法调用
    this.testFunc().then(
      res=>{ console.log(res);},
      error=>{ console.log(error);}
    );

    2. async/await使用方法

    async getScheduleList(selectDate) {
        let response;
        // 这里request为向服务的发请求的方法
        await request(api.getScheduleList, {
            date: selectDate
        }).then(res => {
            response = res;
        });
        return response
    }
    
    init() {
        this.getScheduleList(selectDate).then(res => {
            console.log(res)
        })
    } 
    swichMenu: async function() {
        //点击其中一个 menu
        const num = await  getNum()
        return num
    }
    
    swichMenu().then(res => {
        console.log(res)
    })

    引用:https://blog.csdn.net/superlover_/article/details/79715202

  • 相关阅读:
    (一)linux 系统命令与文件
    python(1)- 初识python
    四、计算机硬件知识整理
    三、计算机硬件历史
    二、网络基础之网络协议
    jmeter while循环使用
    gitlab分支管理
    gitlab基本操作
    JAVA多种向influxDB中插入数据方式
    JAVA类变量和实例变量
  • 原文地址:https://www.cnblogs.com/1285026182YUAN/p/11393522.html
Copyright © 2020-2023  润新知