async/await-摘要:
有时候我们需要同步的写法,即下一步的结果依赖于上一步的请求返回结果,当然我们可以把下一步的写法,放入请求的then
回调中, 但这是不直观的,这里我只简单记录下:
1.比如我们有一个获取默认Id的接口,首次加载调用。
//默认季度
Get_GDPT() {
return new Promise((resolve) => {
this.$api.Sreen.Get_GDPT().then((res) => {
resolve(res);
});
});
},
mounted() {
this.mounted_data(this.rtvRptModel, true);
// 五分钟刷新一次
const reloadTimer = setInterval(() => {
this.mounted_data(this.rtvRptModel, false);
}, 300000);
this.$once('hook:beforeDestroy', () => {
clearInterval(reloadTimer);
});
},
2.全局数据调用的方法,bool为false时默认季度ID不变,刷新/查询都方便。
mounted_data(params, bool) { //bool 表示 首次加载需要调取默认季度接口拿:PurmeetingId (async (params) => { if (bool) { let dataID = await this.Get_GDPT(); this.rtvRptModel.PurmeetingId = dataID; } await this.Get_RCAL(params); await this.Get_RTPSNBL(params); await this.Get_RTPSNSL(params); })(params); },
刷新、查询:
//刷新 refresh() { this.mounted_data(this.rtvRptModel, false); }, // 子通父-默认展示查询 defaultpar(data) { this.rtvRptModel.Sort = data; this.mounted_data(this.rtvRptModel, false); }, // 子通父 查询form popform(data) { const dataPar = data; dataPar.Sort = this.rtvRptModel.Sort; this.rtvRptModel = dataPar; this.mounted_data(this.rtvRptModel, false); },