vue方法同步(顺序)执行:async/await使用

项目中有一个地方需要获取到接口返回值之后根据返回值确定之后执行的步骤,使用async搭配await实现,await函数不能单独使用。方法如下:

                // 车辆布控的详情  通过id 查询  gcbh
                async getBkxx(id){
                    var self = this;
                    var isSuccess = false;
                    await $.ajax({
                            type: "get",
                            url: "/admin/carqc/trackalarm/index/getBkxx",
                            data: { id: id  },
                            success: function(res){
                                if(res.code == '0'){
                                    var data = res.data.data;
                                    self.data_gcbh = data.gcbh;
                                    self.data_hpzl = data.hpzl;
                                    isSuccess = true
                                } else{
                                    console.log('获取布控信息失败:' + res.msg);
                                }
                            }
                        });
                    return isSuccess
                },
                // 车辆布控的详情
                showDeploymentDetails(record, index){
                    // console.log("showDeploymentDetails -> record", record)
                    var self = this;
                    // async函数返回的是一个Promise对象,可以使用then函数添加回调函数
                    self.getBkxx(record.yssjid).then((result) => {
                        if(result){
                            var url = '/admin/carqc/trackalarm/index/getPassCarDetails',
                                hphm = record.hphm,
                                hpzl = self.data_hpzl,
                                gcbh = self.data_gcbh,
                                gcbh = '20330cd8d3f44e6c8c2c87c875309663';
                                id = record.yssjid,
                                total = null,
                                data = null;

                            top.layer.open({
                                type: 2,
                                content: url + '?hphm=' + encodeURI(hphm)+ '&hpzl=' + hpzl + '&gcbh=' + gcbh + '&id=' + id + '&index=' + index + '&total=' + total,
                                title: hphm + '的过车详情',
                                area: ['95%','80%'],
                                success: function(layero, index){
                                    var iframeWin = top[layero.find('iframe')[0]['name']];

                                    // 上下一条操作
                                    iframeWin.getResult = function (value) {
                                        var item = data[value],
                                            _index = value,
                                            _hphm = item.hphm,
                                            _hpzl = item.hpzl,
                                            _gcbh = item.gcbh,
                                            _id = item.id,
                                            _title = _hphm + '的过车详情',
                                            _url = url + '?hphm=' + encodeURI(_hphm)+ '&hpzl=' + _hpzl + '&gcbh=' + _gcbh  + '&id=' + _id+ '&index=' + _index + '&total=' + total;

                                        layer.iframeSrc(index, _url);
                                        layer.title(_title, index)
                                    }
                                }
                            });
                        } else {
                            // 
                        }
                    })

                },

 

你可能感兴趣的:(vue)