当前位置:网站首页>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 12:15:00 【Parade years】
This article has participated in 「 New artist creation ceremony , Start the road of nuggets creation together .
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 , 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
边栏推荐
- Leetcode 1209. Delete all adjacent duplicates II in the string (not in the initial version)
- LinkedList 5-141. 环形链表
- 想学习eTS开发?教你开发一款IQ-EQ测试应用
- Which securities company is the most reliable and safe to open an account
- halcon原理:一维函数function_1d类型【2】
- Halcon knowledge: dyn_ Usage of threshold (scratch detection)
- halcon知识:dyn_threshold的用法(划痕检测)
- ROS knowledge: reading point cloud data files
- DevEco Device Tool 助力OpenHarmony设备开发
- navicat定时任务无效
猜你喜欢

机器学习系列5:距离空间(1)

生态 | 万里数据库与卫士通完成兼容认证 共筑网络安全生态体系

得物多活架构设计之路由服务设计

蓝桥杯单片机(一)——关闭外设及熄灭LED

Getting started with redis - Chapter 4 - data structures and objects - jump table

The median annual salary exceeds 300000, and the salary of the first AI major graduate of Nanjing University is exposed

【零基础微信小程序】基于百度大脑人像分割的证件照换底色小程序实战开发

Halcon knowledge: dyn_ Usage of threshold (scratch detection)
![ROS observation [57]: configure arm robots to grasp things](/img/11/5545298c7b23649df09ff7f116c888.png)
ROS observation [57]: configure arm robots to grasp things

Meta称英安全法可能“扫描所有私人信息” 或侵犯隐私
随机推荐
目前 在哪里开户是最安全正规的?
Oversampling Series II: Fourier transform and signal-to-noise ratio
DevEco Device Tool 助力OpenHarmony设备开发
wc 统计已过时,cloc 每一行代码都有效
学习笔记 scrapy 爬虫框架
halcon原理:相关性匹配
『忘了再学』Shell流程控制 — 39、特殊流程控制语句
Halcon knowledge: dyn_ Usage of threshold (scratch detection)
Hot spot of equity transfer: 93.75% equity transfer of Chongqing Jianke Construction Engineering Quality Inspection Co., Ltd
QT5知识:DNS查询
Design of routing service for multi Activity Architecture Design
【UVM入门 ===> Episode_7 】~ sequence、sequence item、sequencer、driver
Ros2 knowledge (6): principle and practice of coordinate object TF
Linked list 5 - 234 Palindrome linked list
Easy to understand soft route brushing tutorial
Open classes are short videos! Tonight, I will teach you how to realize accurately!
mysql,如何在使用存储过程计算最大值
ROS knowledge: the calling practice of librviz Library
Introduction to redis - Chapter 1 - data structures and objects - simple dynamic string (SDS)
QT5知识:信号和槽的一些要点