How to clear the cache after using keep-alive in vue

How to clear the cache after using keep-alive in vue

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:
  • The keep-alive component in Vue implements caching of multi-level nested routes
  • Detailed explanation of Vue's routing guard and keep-alive life cycle
  • Detailed explanation of keep-alive in Vue
  • Keep-alive multi-level routing cache problem in Vue
  • Detailed explanation of vue3 cache page keep-alive and unified routing processing
  • Analysis of the implementation principle of Vue keep-alive

<<:  MySQL InnoDB row_id boundary overflow verification method steps

>>:  Detailed explanation of the mysqlslap command and syntax for the built-in stress test in MySQL 5.7

Recommend

CnBlogs custom blog style sharing

After spending half the night on it, I finally ma...

Solutions to black screen when installing Ubuntu (3 types)

My computer graphics card is Nvidia graphics card...

Docker file storage path, modify port mapping operation mode

How to get the container startup command The cont...

Detailed explanation of the principle of Docker image layering

Base image The base image has two meanings: Does ...

Mysql | Detailed explanation of fuzzy query using wildcards (like,%,_)

Wildcard categories: %Percent wildcard: indicates...

Implementation of Single Div drawing techniques in CSS

You can often see articles about CSS drawing, suc...

Ubuntu 20.04 turns on hidden recording noise reduction function (recommended)

Recently, when using kazam in Ubuntu 20.04 for re...

Implementation of element input box automatically getting focus

When making a form in a recent project, I need to...

Basic statements of MySQL data definition language DDL

MySQL DDL statements What is DDL, DML. DDL is dat...

Three.js sample code for implementing dewdrop animation effect

Preface Hello everyone, this is the CSS wizard - ...

Summary of MySQL slow log related knowledge

Table of contents 1. Introduction to Slow Log 2. ...

Page Speed ​​Optimization at a Glance

I believe that the Internet has become an increas...

Detailed explanation of MySQL database (based on Ubuntu 14.0.4 LTS 64 bit)

1. Composition and related concepts of MySQL data...