vue axios http请求顺序执行 同步方法

2,303 阅读1分钟
    /****  方法一  
      * 通过定时间进行延时
      setTimeout(() => {  }, 300)
      *
      *
    *****/
    /*initTreeShow() {
      const params = {}
      params.jzbh = this.flowRow.caseNum
      // params.type = this.activeName
      this.initTree(params)

      // ids = JSON.stringify(ids);
      setTimeout(() => {
        let ids = []
        ids = this.defaultMlbh.join(',')
        const param = {}
        // params.list = [];
        param.catalogIds = ids
        param.jzbh = this.flowRow.caseNum
        // params.type = this.activeName
        this.findPreviewFile(param)
      }, 300)
    },*/
    /****  方法二  
      * 通过异步函数变量 接收 
      *
      *
    *****/
    async initTreeShow() {
      const params = {}
      params.jzbh = this.flowRow.caseNum
      // params.type = this.activeName

      let res = await treeCatalog(params)
      if (res.code === 200 || res.code === 'code_200') {
        this.initTreeData = res.data

        this.catalogueData = transData(res.data, 'mlbh', 'fmlbh', 'children')
        this.defaultArray = res.data.filter(ele => ele).map(ele => {
          return ele.id
        })
        this.defaultMlbh = res.data.filter(ele => ele).map(ele => {
          return ele.mlbh
        })
      }
      if(res){
        let ids = []
        ids = this.defaultMlbh.join(',')
        const param = {}
        // params.list = [];
        param.catalogIds = ids
        param.jzbh = this.flowRow.caseNum
        // params.type = this.activeName
        this.findPreviewFile(param)
      }
    },
   /****  方法三 
      * 通过链式函数调用 q请求结果
      *
      *
    *****/
    async initTreeShow() {
      const params = {}
      params.jzbh = this.flowRow.caseNum
      // params.type = this.activeName

      await treeCatalog(params).then(res =>{
        if (res.code === 200 || res.code === 'code_200') {
          this.initTreeData = res.data

          this.catalogueData = transData(res.data, 'mlbh', 'fmlbh', 'children')
          this.defaultArray = res.data.filter(ele => ele).map(ele => {
            return ele.id
          })
          this.defaultMlbh = res.data.filter(ele => ele).map(ele => {
            return ele.mlbh
          })
        }
      }).then(()=>{
        let ids = []
        ids = this.defaultMlbh.join(',')
        const param = {}
        // params.list = [];
        param.catalogIds = ids
        param.jzbh = this.flowRow.caseNum
        // params.type = this.activeName
        this.findPreviewFile(param)
      })
    },

}