我正在使用多個「模塊」,每個模塊都有自己的API調用。大多數終點都很快,但有一對夫婦可能需要幾秒鐘的時間。用Axios取消Vue.js中的多個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它被支持,但我無法讓我的腦袋圍繞如何實現它,因爲我希望。
謝謝!
謝謝,這是我的備份選項,但你幫助解釋它。所有過濾都是通過下拉菜單完成的,所以我現在將禁用它們。希望API調用足夠快,不需要太多,但我知道Chrome已經停滯了幾次。 – Lovelock
是的,我知道它是怎麼回事。我同意,如果用戶錯誤地選擇了錯誤的輸入(下拉選擇),如果花了那麼長時間,等待會很煩人。但是imo防止它更可測試和保證行爲。因爲我不想太依賴取消行爲。但最終這是你的呼叫。祝你好運=) – cassioscabral