【发布时间】:2018-03-11 13:06:48
【问题描述】:
我正在开发一个包含多个“模块”的仪表板,每个模块都有自己的 API 调用。大多数端点都很快速,但有几个可能需要几秒钟。
我有一个日期范围的过滤选项,每次更改时我都会重新运行 API 调用数据。
问题是,如果用户在其他人加载之前不断快速更改日期范围,我不希望用户能够堆叠 API 调用。
我使用单个文件 vue 组件,并为每个 API 调用提供一个方法,然后使用一个方法对它们进行分组和调用。
watch: {
dateFilter: function() {
this.initStatModules();
}
},
methods: {
getCustomers: function() {
var $this = this;
return axios.get(`/api/v1/reports/${$this.team.id}/stats-dashboard/customers?date=${$this.dateFilter}`).then(function(response) {
$this.customers = response.data;
});
},
getBookings: function() {
var $this = this;
return axios.get(`/api/v1/reports/${$this.team.id}/stats-dashboard/bookings`).then(function(response) {
$this.bookings = response.data;
});
},
getTotalRevenue: function() {
var $this = this;
return axios.get(`/api/v1/reports/${$this.team.id}/services-revenue?date=${$this.dateFilter}`).then(function(response) {
$this.totalRevenue = response.data.data.totalRevenue;
});
},
initStatModules: function() {
this.getCustomers();
this.getBookings();
this.getTotalRevenue();
}
}
我希望能够在 watch 或 initStatModules 方法中取消所有待处理的 API 请求。
查看 axios 文档:https://github.com/axios/axios#cancellation 它受支持,但我无法按照自己的意愿实现它。
谢谢!
【问题讨论】: