当前位置: 首页 > news >正文

Vue 缓存机制

1. 在 store.js 中设置全局的 数据存储字段

// 是否缓存keepAlivePage:[],mutations :CHANGE_KEEP_ALIVE(state,data){state.keepAlivePage=data},actions:{//类似 mothods,异步// 页面缓存setKeepAlivePage({commit},data){commit("CHANGE_KEEP_ALIVE",data)},}

2.在 自定义的 set.js 中设置全局方法

importmomentfrom'moment'exportdefault{install(Vue){// 增加缓存页面Vue.prototype.addKeepAlive=function(componentName){letalivePage=this.$store.state.keepAlivePage.slice(0)alivePage.push(componentName)alivePage=Array.from(newSet(alivePage))this.$store.dispatch('setKeepAlivePage',alivePage)console.log(this.$store.state.keepAlivePage,"当前缓存的页面1")}// 删除缓存页面Vue.prototype.deleteKeepAlive=function(componentName){letalivePage=this.$store.state.keepAlivePage.slice(0)alivePage.forEach((item,index)=>{if(item==componentName){alivePage.splice(index,1)}})alivePage=Array.from(newSet(alivePage))this.$store.dispatch('setKeepAlivePage',alivePage)console.log(this.$store.state.keepAlivePage,"当前缓存的页面2")}}}

3.在 App.vue 中设置缓存

<keep-alive:include="keepAlivePage"><router-view></router-view></keep-alive>computed:{...mapState(["keepAlivePage"]),},

4. 在页面中使用

beforeRouteLeave(to,from,next){const_this=this;console.log(to.name,from.name,"beforeRouteLeave")// 如果跳转到详情页面走缓存if(to.name=="productetails"){_this.addKeepAlive(from.name);}else{_this.deleteKeepAlive(from.name);}next()},或者beforeRouteLeave(to,from,next){const_this=this;// console.log(to.name, from.name, "beforeRouteLeave")// console.log(_this.$options.name, "beforeRouteLeave")// 如果跳转到详情页面走缓存if(to.name=="upcomplaintDetail"||to.name=="storeexamine"||to.name=="dealerexamine"||to.name=="sbuexamine"||to.name=="masterexamine"){_this.addKeepAlive(_this.$options.name);}else{_this.deleteKeepAlive(_this.$options.name);}next()},
http://www.jsqmd.com/news/526756/

相关文章:

  • agent 杂谈
  • 【MCP协议性能突围白皮书】:20年架构师实测17项关键指标,REST API已落后3.8倍?
  • 低代码平台集成AI能力:在Dify中快速调用BERT文本分割模型
  • CentOS 6.4开机卡在图形界面?3种方法快速切换到命令行模式
  • 亲测推荐:黑丝空姐-造相Z-Turbo,小白友好的AI绘图神器
  • WiFlyInterface嵌入式Wi-Fi模块Socket封装库详解
  • Fish-Speech 1.5实战分享:用它为我的PPT添加语音解说
  • 计算机毕业设计springboot基于的就业推荐系统 基于Spring Boot框架的求职招聘智能撮合与人才推荐系统开发 Spring Boot驱动的个性化职业发展与岗位精准匹配系统构建
  • OpenClaw混合部署:Qwen3-VL:30B本地+云端算力动态调配
  • 探索基于西门子S7 - 200 PLC和MCGS组态画面的全自动洗衣机控制系统
  • AI 时代必懂的基础概念(扫盲篇)
  • 74HC590硬件计数器原理与Arduino工程实践
  • 科技成果转化效率低,如何有效提升?
  • python基于微信小程序的物流仓储管理系统设计与实现
  • 老旧系统Python支持解决方案:让Windows 7焕发新活力
  • 前端密码安全实践:MD5加盐加密的深度解析与应用
  • 4步搞定黑苹果系统:OpCore-Simplify让你的macOS安装变得简单
  • STM32H743VIT6上RT-Thread网络驱动踩坑记:从CubeMX配置到LAN8720A驱动移植的完整流程
  • SpringUtil获取Spring容器对象
  • Qwen3-ForcedAligner-0.6B提示工程:提升专业术语识别准确率的技巧
  • 汉字转拼音工具实测!4个免费款,告别手动查字典
  • 风向变了:评职称不看论文数量了?老师们一看“代表作”头更懵了:到底什么样的成果才算高质量学术成果?
  • 用MoveIt玩转机械臂:从RVIZ交互控制到真实硬件对接全流程
  • Win11Debloat:5分钟让你的Windows 11系统焕然一新
  • Leather Dress Collection 入门必看:从模型下载到第一次成功调用
  • 告别刹车点头!用Carsim联合仿真,手把手教你调校半主动悬架的俯仰控制
  • 终极指南:使用OpenCore Legacy Patcher让旧款Mac焕发新生
  • 两台I型NPC逆变器基于VSG控制实现功率均分的探索
  • 终端开发者利器:OpenClaw操控百川2-13B实现CLI智能补全
  • SciPy与MATLAB数组操作对比指南