preface
As the current projects are based on TAB navigation, the purpose is to remember the search term or form value of the previous page when the page jumps, reduce requests and increase efficiency. The first response is keep-alive, and then my nightmare comes π«. Ps: see the cover picture come in remember to collect oh π
Keep alive – concept
Properties:
include
onlyThe name of theMatching components are cached π(the name refers to the name defined in the component).exclude
. Any component whose name matches will not be cached π§΅.max
. Maximum number of component instances can be cached π₯.
Usage:
when wrapping dynamic components, inactive component instances are cached rather than destroyed. Like
is an abstract component π€ : it does not render a DOM element on its own, nor does it appear in the component’s parent chain π₯§.
When a component is switched within
, its activated and deactivated lifecycle hook functions will be executed π, since we may need to cache the entire routing page, so
will be cached π΄.
Order of execution:
- The sequence in which the hooks are triggered when the page is first entered
created-> mounted-> activated
Triggered when exitingdeactivated
. Only fires when reentering (forward or backward)activated
π¦Ό.
The router – view concept
<router-view>
A component is a functional component that renders the matching component π¨ for a given path. Rendered components<router-view>
It can also contain its own<router-view>
, which will render the component for the nested path π€΄.
Router-view key attribute (each component has a key attribute. Vue will reuse the same component and use a different key if it needs to re-render) π
- Don’t set
router-view
ηkey
attributeSince Vue will reuse the same component, namely /page/1 => /page/2 or /page? id=1 => /page? Created/Mounted (π); π± Add beforeRouteUpdate (π₯―) to the route component.
- Set up the
router-view
ηkey
Attribute values for$route.path
/page/1 => /page/2; /page/1 => /page/2; π΅ => /page/2; id=1 => /page? Id =2,π¦ Because the $route.path of the two routes is the same, the component π₯‘ is reused just as if the key attribute is not set.
- Set up the
router-view
ηkey
Attribute values for$route.fullPath
/page/1 => /page/2; /page/1 => /page/2;
From/page? id=1 => /page? If id=2, the component is forced not to reuse π₯ͺ because the two routes have different $route.fullPath.
writing
Vue2.0 spelled
For example, an item references multiple components that modify the currentTabComponent value by corresponding events to achieve a dynamic component cache. <keep-alive> < Component V-bind :is="currentTabComponent"></component> </keep-alive> # Package route # a,b for component name <keep-alive :include=[a,b]> <router-view :key="$route.fullPath"></router-view> </keep-alive>Copy the code
Vue3.0 spelled
For example, an item references multiple components that modify the currentTabComponent value by corresponding events to achieve a dynamic component cache <keep-alive> < Component V-bind :is="currentTabComponent"></component> </keep-alive> # Key to bind to component <router-view v-slot="{ Component }"> <keep-alive :include=[a,b]> <component :is="Component" :key="$route.fullPath"/> </keep-alive> </router-view>Copy the code
The project of actual combat
Unillustrated egg π€, a brief look at the result we finally achieved
Do not usekeep-alive
The effect of
- All pages are not cached, and any jumps involving routing are executed by the current component
mounted
, π there is some performance cost, causing the loaded data to be reloaded for rendering and the previously filled data to be not cached during the overhead navigation switch π§§.
All of themkeep-alive
The effect of
- The first time of each page is executed
mounted
, 𧀠a subsequent visit to the page will enter the cache, causing us to close the page without performing component destruction, loading only the first loaded data, the new data is not rendering π¦Ί.
Using thekeep-alive
throughinclude
andvuex
Control the cache
In this way we can dynamically control cached components.
- When the page is closed we remove components that do not need caching π±. After removal the component’s destruction life cycle π₯ is performed. The page is re-rendered before opening and sometimes this is perfect π.
- But things always go the other way, like the editor π΄ in the image above, who are different routes but point to the same component
name
It must be the same) π€’, which would cause us to open two edit pages at the same time, loading the data for the first time each time π£.
Final solution: keep-aliveinclude+ router – viewkey
We already mentioned the role of key in router-view π, So we just need to bind the router-view key to “$route.fullPage”π₯ if it’s vue3.x we need to bind the key to component π₯ so that when we click on the edit of a different item in the list each time we walk through the latest data π―, At the same time, when we switch the two edit pages above the include in keep-alive, we will go through the cache π» instead of loading the page π΅.
vue3.x
Set up thename
< Script Setup > is a compile-time syntactic sugar for using composite apis in single-file components (SFC).
- We do not need to use data when defining variables
- Methods can define variables and methods directly within tags without using methods.
Include in kepp-alive requires the component name. How do you define the name variable in vue3.
In Vue3. X, the name of the component is inferred from the file name, which means that the name of the component corresponds to the nameπ. If you are stubborn π€£, there is a way to define the component name: π―.
- Write two
script
The label- If the project is built through Vite, it can be built through plug-ins (
vite-plugin-vue-setup-extend
).
1. Multiple Script tags
<script>
export default {
name: 'demo'
}
</script>
<script setup>
// do something...
</script>
Copy the code
vite
Building plug-in references
Ts import {defineConfig} from 'vite' import VueSetupExtend # install NPM I vite-plugin-vue-setup-extend -d # configure vite.config from 'vite-plugin-vue-setup-extend' export default defineConfig({ plugins: <script lang="ts" setup name="demo"> </script>Copy the code
conclusion
At the beginning, it was because I didn’t have a thorough grasp of keep-alive, but I only knew what it was, π, which was used in real projects that I found many problems. Goodbye, brothers. Next time, I will see π₯€.
Refer to the article
How are components defined by Vue3.2 Script Setup named?
Vue2 website
Vue3 website