vue keepalive 动态设置缓存

场景:A首页、B列表页、C详情页
B---->C 缓存‘列表1’详情的数据
A---->C 读取‘列表1’详情的数据
B---->C
(希望清除‘列表1’的缓存,变成缓存‘列表2’详情的数据)
A---->C 问题出现了,读取的竟然还是‘列表1’的缓存数据。

main.vue
1 2                 <!--缓存的视图-->
3                 <keep-alive>
4                     <router-view v-if="$route.meta.keepAlive">
5                 </router-view>
6                 </keep-alive>
7                 <!--不缓存的视图-->
8                 <router-view v-if="!$route.meta.keepAlive">
9                 </router-view>                        

router.js

 1 { meta:{ keepAlive:true },path:'editNotice',title:'编辑生产通知单',name:'editNotice',icon:'ios-paper', component: () => import('@/views/production-order/edit-notice.vue')}, 

缓存的页面刷新缓存的操作

 1      activated(){    //keepAlive组件激活才会触发的钩子,用于重新获取数据进而保存更新的缓存
 2             this.againGetData = this.$route.query.activated;
 3             if(this.againGetData === true){
20                 //获取详情的请求
21                 this.getDetailHttp()
22             };
23             this.$route.query.activated = false  //初始化路由的参数
 24 },

 

转载于:https://www.cnblogs.com/web-zqk/p/9675259.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3 的 `keep-alive` 组件提供了动态缓存的功能,可以根据组件的属性值动态缓存组件。 在 Vue2 中,我们可以使用 `include` 和 `exclude` 属性来控制缓存哪些组件和不缓存哪些组件。但是这种方式无法根据组件的属性值进行动态缓存。 在 Vue3 中,我们可以使用 `v-bind` 指令来动态绑定 `keep-alive` 组件的 `include` 和 `exclude` 属性,实现动态缓存的功能。例如: ```html <template> <div> <button @click="toggle">Toggle</button> <keep-alive :include="includeList" :exclude="excludeList"> <router-view /> </keep-alive> </div> </template> <script> export default { data() { return { includeList: [], excludeList: [], show: false, }; }, methods: { toggle() { this.show = !this.show; if (this.show) { this.includeList.push("ComponentA"); this.excludeList = this.excludeList.filter((name) => name !== "ComponentA"); } else { this.excludeList.push("ComponentA"); this.includeList = this.includeList.filter((name) => name !== "ComponentA"); } }, }, }; </script> ``` 在上面的代码中,我们使用 `includeList` 和 `excludeList` 数组来动态控制缓存哪些组件和不缓存哪些组件。在 `toggle` 方法中,根据 `show` 属性的值来决定是否缓存 `ComponentA` 组件。如果 `show` 为 `true`,则将 `ComponentA` 添加到 `includeList` 中,并从 `excludeList` 中删除;如果 `show` 为 `false`,则将 `ComponentA` 添加到 `excludeList` 中,并从 `includeList` 中删除。 这样就可以根据组件的属性值动态缓存组件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值