当前位置:网站首页>An idea of using keep alive to cache data in vue3 form pages
An idea of using keep alive to cache data in vue3 form pages
2022-06-23 08:58:00 【Parade years】
functional requirement
Such a requirement is often encountered in projects : There is a form page , There are a lot of fields , Some form items need to jump to a new page ( For example, select a contact ), At this time, you need to cache the data of the form page , After selecting the contact, you can return to the form page , The previously filled data will not be lost .
Functional analysis
Realize such a requirement , I thought of it the first time Vue Built in components of keep-alive, But simply use keep-alive There's a problem : Every time you enter the form page and want to resubmit data , The last data will be displayed . The actual requirement is that each time you enter the form, it will be a new page , This requires that the cached data be cleared after the submission is completed . In the use of Vue2 When , In official issue Zone found a solution [ hope keep-alive It can add the function of dynamically deleting cached components ](https://github.com/vuejs/vue/issues/6509#issuecomment-403693256), But the solution is unofficial , Interested guys can learn about . Unfortunately, this solution is in Vue3 It can't be used , because #destroy It's abandoned . Let's talk about how I use Vue3 A way of thinking
precondition
- key attribute : In the list item Use Unique key, Minimal re rendering dom, Of course key Different , It will definitely trigger the replacement , See official documentation
- is attribute
- When using non single file components , Can put the original html Elements are rendered as Vue Components
- Use in single file components , Usually used in dynamic components
ComponentOn
- router-view:
<router-view>Exposed av-slotAPI, The main use of<transition>and<keep-alive>Component to wrap your routing component .
Code writing
stay App.vue Use in
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>Here and Vue2 Their writing is different ,Vue2 Yes, it is
keep-aliveb The parcelrouter-view, Through the... In the routing table meta AddkeepAliveThe standard adapter determines which component uses the cache , Through dynamickeyTo indirectly clear the cacheThe code before entering the form page :
const goSave = () => { router.push({ name: 'proposal-save', params: { keepKey: new Date().getTime() } }) }Each time you enter the form page, a new key, To ensure that every time you enter the form page, you will replace it with the original cache
The form page enters the select contact page :
const goContact = () => { router.push({ name: 'proposal-contact', params: { keepKey: route.params.keepKey, ids } }) }When entering the page from the form, the carrier brings the dynamic information from the previous page key
The code for returning to the form page after selecting the address book page
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() })In the local route navigation guard , Judge the dynamics of the form page according to the conditions key, Send it back , The components are the same when returning (key Also the same ) The previous data will be preserved , To meet the needs mentioned at the beginning
summary
This can only be said to be a solution to the problem , But it should not be the most elegant and the best , If a big man has a better solution , Also hope to share
边栏推荐
- 社区文章|MOSN 构建 Subset 优化思路分享
- @Response
- Third party payment in the second half: scuffle to symbiosis
- Linux MySQL installation
- How to use the template library of barcode label software
- 2022-06-22:golang选择题,以下golang代码输出什么?A:3;B:1;C:4;D:编译失败。
- Hongmeng reads the resource file
- 2022.6.22-----leetcode. five hundred and thirteen
- How to restore visualizations and dashboards after kibana rebuilds the index
- 自定义标签——jsp标签基础
猜你喜欢

JS mask important data of ID card and mobile phone number with * *

点击添加下拉框

3. caller service call - dapr

Quartz Crystal Drive Level Calculation

Community article | mosn building subset optimization ideas sharing

Flink error --caused by: org apache. calcite. sql. parser. SqlParseException: Encountered “time“

【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启

js 用**遮罩身份证以及手机号的重要数据

Install a WGet for your win10

"Coach, I want to play basketball" -- AI Learning Series booklet for students who are making systems
随机推荐
438. Find All Anagrams in a String
Implementing an open source app store with swiftui
[cloud computing] GFS ideological advantages and architecture
528. Random Pick with Weight
Community article | mosn building subset optimization ideas sharing
Unity grid programming 08
173. Binary Search Tree Iterator
Assembly (receive several n-digit decimal values (0~65535) from the keyboard and display their sum in different base numbers.)
636. Exclusive Time of Functions
65. Valid Number
297. Serialize and Deserialize Binary Tree
Summary ranges of leetcode topic resolution
Why is the easycvr Video Fusion platform offline when cascading with the Hikvision platform? How to solve it?
Leetcode topic analysis 3sum
类型从属名称的使用必须以“typename”为前缀
125. Valid Palindrome
力扣之滑动窗口《循序渐进》(209.长度最小的子数组、904. 水果成篮)
C # advanced learning -- virtual method
点击添加下拉框
6、 Web Architecture Design