vue asyncとawaitを使用してリクエスト非同期の問題を解決し、リクエストが結果を返して次のコードを実行することを確認します.

23906 ワード

参照vueリクエストの非同期問題の解決
  • 自己コード
  • afterRequest(result) {
       //      ,                  
       //            
       this.getDataEveryRepGroup(result).then(res=>{
           const _self = this;
    
           //           
           for (let i = 0; i < this.repGroupData.length; i++) {
               let sum = function (x, y) {
                   return x + y;
               };  //    
               let square = function (x) {
                   return x * x;
               };  //            
               let data = this.repGroupData[i].countArr;   //      
               let mean = data.reduce(sum) / data.length;
               let deviations = data.map(function (x) {
                   return x - mean;
               });
               //   
               let stddev = Math.sqrt(deviations.map(square).reduce(sum) / (data.length - 1));
               this.repGroupData[i].stddev = stddev;
           }
    
           //     
           let stddevSum = 0;
           for (let i = 0; i < this.repGroupData.length; i++) {
               stddevSum +=  this.repGroupData[i].stddev;
           }
           //     
           let stddevAvg = stddevSum / this.repGroupData.length;
    
           for (let i = 0; i < this.repGroupData.length; i++) {
               //            
               if (this.repGroupData[i].stddev > stddevAvg) {
                   //         ,  
                   _self.highlightRepGroup.push(this.repGroupData[i].repGroup)
               }
           }
           //        
           console.log("this.highlightRepGroup-        ")
           console.log(this.highlightRepGroup)
    
           //         (     )
           let highlightIndex = []
           for (let i = 0; i < this.highlightRepGroup.length; i++) {
               for (let j = 0; j < result.length; j++) {
                   if (this.highlightRepGroup[i] === result[j].repGroupName) {
                       highlightIndex.push(j);
                   }
               }
           }
    
           console.log("          ")
           console.log(highlightIndex)
    
           let _options = JSON.parse(JSON.stringify(this.chartOptions));
           let _temp_options = barRender(this.dimensionData, this.measureData, _options, result, this.styleSel);
           if (_temp_options) {
               _self.chartOptions = _temp_options;
           }
    
           //       
           for (let i = 0; i < highlightIndex.length; i++) {
               _self.chartOptions.series[0].data[highlightIndex[i]] = {
                   value: _self.chartOptions.series[0].data[highlightIndex[i]],
                   itemStyle: {
                       color:'#E062AE'
                   }
               }
           }
    
           this.reLoad = false;
           this.$nextTick(() => {
               this.reLoad = true;
           });
    
       });
    },
    
    //       
    async getDataEveryRepGroup(result){
        //             ,    
        let params = {}
        this.initParams(params);
    
        // screen      
        let repGroupScreen = [{
            fieldName: "repGroupName",
            field: "repGroupName",
            operation: "=",
            iValue: "zuoxizu",
            relation: ""
        }]
        params.screen.push(repGroupScreen)
    
        const _self =this;
        //  iValue
        let paramsArr = []
        for (let i = 0; i < result.length; i++) {
            let repGroup = result[i].repGroupName;
            let _params = JSON.parse(JSON.stringify(params))
            _params.screen[params.screen.length - 1][0].iValue = repGroup
            paramsArr.push(_params);
        }
    
        for (let i = 0; i < paramsArr.length; i++) {
            await getChartData(paramsArr[i]).then(res => {
                //          
                console.log(res)
                let countArr = []
                for (let j = 0; j < res.length; j++) {
                    countArr.push(res[j].count);
                }
                let data = {
                    "repGroup" : result[i].repGroupName,
                    "countArr" : countArr
                }
                _self.repGroupData.push(data)
            })
        }
    
        console.log("this.repGroupData1")
        console.log(this.repGroupData)
    }