我正在用Vue.js开发一个PWA.当用户启动它时,需要来自另一个应用程序的一些信息.为此,我正在使用axios:
let url = 'url';
axios.get(url).then((response) => {
callback(response.data)
})
Run Code Online (Sandbox Code Playgroud)
只要用户在线,它就能正常工作.如果网络连接正常,则应通过URL检索数据,如果没有Internet连接,则应从缓存加载数据.这怎么可能?
03b*_*gun 11
您可以查看此扩展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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11997 次 |
| 最近记录: |