记录vue+axios+adapter实现数据缓存

import cache from '@/utils/cache'

//request拦截器

service.interceptors.request.use(config => {
  //只对部分接口数据缓存
  if ((config.url.indexOf('system/assetequipmentsort/jsoncode') > -1){
    let urls = getUrl(config)
    let value = cache.session.getJSON(urls)
    if (value){
      console.log(urls,value)
      config.adapter = ()=>{
        return new Promise((resolve, reject) => {
          resolve({
            config:config,
            headers:config.headers,
            request:config.request,
            data: value,
            status: 200,
            statusText: 'OK'
          })
        })
      }
    }
  }
})

//响应拦截器

service.interceptors.response.use(res => {
 //只对部分接口数据缓存
if ((res.config.url.indexOf('system/assetequipmentsort/jsoncode') > -1){
  if (res.data){
    let urls = getUrl(res.config)
    let value = cache.session.getJSON(urls)
    if (!value){
      cache.session.setJSON(urls,res.data)
    }
  }
}
  return res.data
})

//处理url作为key

function getUrl(config) {
  let url = config.url
  if (config.params && config.method == 'post'){
    if (url.indexOf('?') > -1) {
      url = url + '&';
    } else {
      url = url + '?';
    }
    for (const propName of Object.keys(config.params)) {
      const value = config.params[propName];
      var part = encodeURIComponent(propName) + "=";
      if (value !== null && typeof (value) !== "undefined") {
        if (typeof value === 'object') {
          for (const key of Object.keys(value)) {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + "=";
            url += subPart + encodeURIComponent(value[key]) + "&";
          }
        } else {
          url += part + encodeURIComponent(value) + "&";
        }
      }
    }
    url = url.slice(0, -1);
  }
  return url
}
//cache.js
const sessionCache = {
  set (key, value) {
    if (!sessionStorage) {
      return
    }
    if (key != null && value != null) {
      sessionStorage.setItem(key, value)
    }
  },
  get (key) {
    if (!sessionStorage) {
      return null
    }
    if (key == null) {
      return null
    }
    return sessionStorage.getItem(key)
  },
  setJSON (key, jsonValue) {
    if (jsonValue != null) {
      this.set(key, JSON.stringify(jsonValue))
    }
  },
  getJSON (key) {
    const value = this.get(key)
    if (value != null) {
      return JSON.parse(value)
    }
  },
  remove (key) {
    sessionStorage.removeItem(key);
  }
}
const localCache = {
  set (key, value) {
    if (!localStorage) {
      return
    }
    if (key != null && value != null) {
      localStorage.setItem(key, value)
    }
  },
  get (key) {
    if (!localStorage) {
      return null
    }
    if (key == null) {
      return null
    }
    return localStorage.getItem(key)
  },
  setJSON (key, jsonValue) {
    if (jsonValue != null) {
      this.set(key, JSON.stringify(jsonValue))
    }
  },
  getJSON (key) {
    const value = this.get(key)
    if (value != null) {
      return JSON.parse(value)
    }
  },
  remove (key) {
    localStorage.removeItem(key);
  }
}
export default {
  /**
   * 会话级缓存
   */
  session: sessionCache,
  /**
   * 本地缓存
   */
  local: localCache
}

sessionCache只能储存4M数据,储存大量数据需要用indexedDB

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一个用于构建用户界面的渐进式框架,而axios是一个基于Promise的HTTP库,用于发送异步请求。在Vue中使用axios可以实现与后端服务器进行数据交互。 首先,在Vue项目中引入axios。可以通过npm安装axios,并在项目中引入: ```javascript import axios from 'axios' ``` 然后,在Vue组件中使用axios发送请求。可以使用axios的get、post等方法发送HTTP请求,并通过then和catch处理返回的数据: ```javascript axios.get('/api/data') // 发送GET请求 .then(response => { // 处理返回的数据 this.data = response.data }) .catch(error => { // 处理请求错误 console.log(error) }) ``` 接下来,使用返回的数据更新Vue组件的数据。可以将返回的数据保存在Vue实例的data属性中,然后在模板中使用该数据: ```javascript export default { data() { return { data: null } } } ``` ```html <template> <div> <p>{{data}}</p> </div> </template> ``` 最后,使用echarts将数据可视化。可以在Vue生命周期的钩子函数中初始化echarts实例,并通过setData方法更新数据: ```javascript import echarts from 'echarts' export default { mounted() { // 初始化echarts实例 const myChart = echarts.init(document.getElementById('chart')) // 设置数据 myChart.setOption({ series: [{ data: this.data }] }) } } ``` 以上是使用Vue+axios+echarts进行数据交互和可视化的基本步骤。使用axios发送请求获取数据,然后使用echarts将数据进行可视化展示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值