我正在用Vue.js开发一个PWA。当用户启动它时,需要来自另一个应用程序的一些信息。为此,我正在使用 axios:
let url = 'url';
axios.get(url).then((response) => {
callback(response.data)
})
只要用户在线,它就可以正常工作。 如果网络连接正常,则应通过 URL 检索数据,如果没有互联网连接,则应从缓存中加载数据。这怎么可能?
您可以查看
此扩展 https://github.com/kuitos/axios-extensions
这是基本的使用示例,希望对您有所帮助
import axios from 'axios';
import { cacheAdapterEnhancer } from 'axios-extensions';
const http = axios.create({
baseURL: '/',
headers: { 'Cache-Control': 'no-cache' },
// cache will be enabled by default
adapter: cacheAdapterEnhancer(axios.defaults.adapter)
});
http.get('/users'); // make real http request
http.get('/users'); // use the response from the cache of previous request, without real http request made
http.get('/users', { cache: false }); // disable cache manually and the the real http request invoked