What is keepalive? In normal development, some components do not need to be initialized multiple times. At this time, we need to persist the components so that the status of the components remains unchanged and the components will not be reinitialized when displayed next time. In other words, keepalive is a built-in component of Vue that allows the included components to retain their state or avoid re-rendering. This is called component caching Basic Usage <keep-alive> <component /> //your component</keep-alive> Requirement: When entering the detail page from the list page and then returning to the list page, the query conditions are retained, but when switching to other tabs, the query conditions are cleared. Solution: It is very simple to retain the query conditions, just introduce keep-alive directly, but if you want to clear them, Vue itself does not have an API to clear them directly, so you have to handle it separately. Reference article: http://aspedrom.com/5HD5 router/index, intercepts the route and processes it: beforeRouteLeave:function(to, from, next){ // Add keep-alive clearing when leaving the route if (from && from.meta.rank && to.meta.rank && from.meta.rank == to.meta.rank) {//The judgment here is if you return to the previous layer. You can change the judgment logic here according to your own business and decide whether to destroy the cache of this layer. if (this.$vnode && this.$vnode.data.keepAlive) { if (this.$vnode.parent && this.$vnode.parent.componentInstance && this.$vnode.parent.componentInstance.cache) { if (this.$vnode.componentOptions) { var key = this.$vnode.key == null ? this.$vnode.componentOptions.Ctor.cid + (this.$vnode.componentOptions.tag ? `::${this.$vnode.componentOptions.tag}` : '') : this.$vnode.key; var cache = this.$vnode.parent.componentInstance.cache; var keys = this.$vnode.parent.componentInstance.keys; if (cache[key]) { if (keys.length) { var index = keys.indexOf(key); if (index > -1) { keys.splice(index, 1); } } delete cache[key]; } } } } this.$destroy(); } next(); }, Also add meta to the route: { //Account list path: '/account', name: 'account', component: () => import('../views/account/index.vue'), meta: { title: 'Account list' ,rank:1.5} }, { //Add account path: '/accountadd', name: 'accountadd', component: () => import('../views/account/add.vue'), meta: { title: 'Add account' ,rank:2.5} }, { // Edit account path: '/accountedit/:id', name: 'accountedit', component: () => import('../views/account/add.vue'), meta: { title: 'Edit account' ,rank:2.5} }, { // Role list path: '/role', name: 'role', component: () => import('../views/role/index.vue'), meta: { title: 'Role List' ,rank:1.5} }, Summarize This is the end of this article about clearing the cache after using keep-alive in Vue. For more relevant keep-alive cache clearing content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL InnoDB row_id boundary overflow verification method steps
After spending half the night on it, I finally ma...
My computer graphics card is Nvidia graphics card...
When programmers do TypeScript/JavaScript develop...
How to get the container startup command The cont...
Base image The base image has two meanings: Does ...
Wildcard categories: %Percent wildcard: indicates...
You can often see articles about CSS drawing, suc...
Recently, when using kazam in Ubuntu 20.04 for re...
When making a form in a recent project, I need to...
MySQL DDL statements What is DDL, DML. DDL is dat...
<br />The page uses UTF8 encoding, and the h...
Preface Hello everyone, this is the CSS wizard - ...
Table of contents 1. Introduction to Slow Log 2. ...
I believe that the Internet has become an increas...
1. Composition and related concepts of MySQL data...