当前位置:网站首页>Vue3表单页面利用keep-alive缓存数据的一种思路
Vue3表单页面利用keep-alive缓存数据的一种思路
2022-06-23 08:38:00 【parade岁月】
功能需求
项目中经常遇到这样一种需求:有一个表单页面,里面有大量的字段,有些表单项需要跳转新页面(比如选择联系人等),这个时候就需要缓存表单页面的数据,从而在选择完联系人返回表单页面,之前填写的数据不会丢失。
功能分析
实现这样的需求,第一时间就想到了Vue的内置组件keep-alive,但是仅仅简单的使用keep-alive会带来一个问题:每次进入表单页面希望重新提交数据的时候,都会显示上次的数据。而实际的需求是每次进入表单都是一个新页面,这就要求提交完成后要清除缓存的数据。在使用Vue2的时候,在官方的issue区找到了一种解决方法[希望keep-alive能增加可以动态删除已缓存组件的功能](https://github.com/vuejs/vue/issues/6509#issuecomment-403693256),但是解决方案是非官方的,有兴趣的小伙按可以了解一下。不幸的是这种解决方案在Vue3不能使用了,因为#destroy被废弃了。下面说一下我使用Vue3时的一种思路
前置条件
- key属性:在列表中的item使用唯一的key,可以最小化的重新渲染dom,当然key不同,肯定会触发替换,参见官方文档
- is属性
- 当使用非单文件组件时,可以把原生html元素渲染成Vue组件
- 在单文件组件使用,通常用在动态组件
Component上
- router-view:
<router-view>暴露了一个v-slotAPI,主要使用<transition>和<keep-alive>组件来包裹你的路由组件。
代码编写
在App.vue中使用
keep-alive<router-view v-slot="{ Component, route }"> <keep-alive :max="5"> <component :is="Component" :key="route.params.keepKey ? route.params.keepKey : route.path" v-if="$route.meta.keepAlive" v-wechat-title="$route.meta.title" /> </keep-alive> <component :is="Component" :key="$route.name" v-if="!$route.meta.keepAlive" v-wechat-title="$route.meta.title" /> </router-view>这里和Vue2的写法不太一样,Vue2是用
keep-aliveb包裹router-view,通过在路由表的meta中添加keepAlive标适确定哪个组件使用缓存,通过动态的key来间接的到达清除缓存的目的进入表单页面的之前的代码:
const goSave = () => { router.push({ name: 'proposal-save', params: { keepKey: new Date().getTime() } }) }每次进入表单页面都会使用新的key,来保证每次进入表单页面都会替换到原来的缓存
表单页面进入选择联系人页面:
const goContact = () => { router.push({ name: 'proposal-contact', params: { keepKey: route.params.keepKey, ids } }) }从表单进入页面时携带者从上个页面带过来的动态key
通讯录页面选择完成返回表单页面的代码
onBeforeRouteLeave((to, from, next) => { if (to.name == 'proposal-save') { to.params.keepKey = route.params.keepKey to.params.selectedData = selectedData.value } else { to.params.keepKey = undefined } next() })在局部路由导航守卫里,根据条件判断把表单页面带过来的动态key,再回传过去,这样返回的时候由于组件相同(key也相同)会保留之前的数据,从而来满足开篇提到的需求
总结
这只能说是一种解决问题的思路,但应该不是最优雅最好的,如果有大佬有更好的解决方案,还希望可以分享出来
边栏推荐
- Dongyuhui, the "square face teacher", responded that the popularity was declining: do a good job of live broadcasting of agricultural products to benefit farmers and consider supporting education
- Detailed explanation of srl16e in xilinxffpga
- [qnx hypervisor 2.2 user manual]6.2 network
- Which one is better for rendering renderings? 2022 latest measured data (IV)
- 通信方式总结及I2C驱动详解
- Implementing an open source app store with swiftui
- The most commonly used 5-stream ETL mode
- [operating steps] how to set the easynvr hardware device to be powered on without automatic startup?
- 523. Continuous Subarray Sum
- Assembly (receive several n-digit decimal values (0~65535) from the keyboard and display their sum in different base numbers.)
猜你喜欢

Hongmeng reads the resource file
![[paper notes] catching both gray and black swans: open set supervised analog detection*](/img/52/787b25a9818cfc6a1897af81d41ab2.png)
[paper notes] catching both gray and black swans: open set supervised analog detection*

Install a WGet for your win10

点云库pcl从入门到精通 第十章

Keng dad's "dedication blessing": red packet technology explosion in Alipay Spring Festival Gala

【云计算】GFS思想优势以及架构

“方脸老师”董宇辉再回应热度下降:把农产品直播做好让农民受益 考虑去支教

Testing -- automated testing selenium (about API)

'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册

986. Interval List Intersections
随机推荐
Leetcode topic analysis count primes
6月《中国数据库行业分析报告》发布!智能风起,列存更生
Assembly (receive several n-digit decimal values (0~65535) from the keyboard and display their sum in different base numbers.)
Install a WGet for your win10
kibana 重建index后,如何恢复Visualizations和 Dashboards
How to sort a dictionary by value or key?
如何评价代码质量
Summary ranges of leetcode topic resolution
[cloud computing] GFS ideological advantages and architecture
How thingjs enables low threshold 3D visualization development
Use newbeecoder UI implements data paging
Interpretation of the most dirty technology in history, I can understand 60 it terms in seconds
636. Exclusive Time of Functions
usb peripheral 驱动 - configfs
1、 Software architecture evaluation
MQTT+Flink实现实时消息的订阅与发布
【学习资源】理解数学和热爱数学
【云计算】GFS思想优势以及架构
125. Valid Palindrome
Object. Defineproperty() and data broker