使用 axios.get() 时,可以使用第二个参数进行缓存。例如:
axios.get('/api/data', { cache: true })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
当然,如果要使用 axios.spread() 来处理多个请求的返回值,则可以自定义缓存。例如:
const cache = {};
axios.all([
axios.get('/api/data1'),
axios.get('/api/data2')
])
.then(axios.spread((response1, response2) => {
cache['data1'] = response1.data;
cache['data2'] = response2.data;
}))
.catch(error => {
console.error(error);
});
// 假设 data1 和 data2 分别是两个请求的返回值
console.log(cache['data1']);
console.log(cache['data2']);