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

Solve the problem of Mac Docker x509 certificate

question Recently I needed to log in to a private...

Solutions for high traffic websites

First: First, confirm whether the server hardware ...

js to write the carousel effect

This article shares the specific code of js to ac...

Detailed explanation of Strict mode in JavaScript

Table of contents Introduction Using Strict mode ...

Full steps to create a password generator using Node.js

Table of contents 1. Preparation 2. Writing comma...

Basic usage of find_in_set function in mysql

Preface This is a new function I came across rece...

What are the benefits of semantic HTML structure?

one: 1. Semantic tags are just HTML, there is no ...

Use vertical-align to align input and img

Putting input and img on the same line, the img ta...

Mysql transaction concurrency problem solution

I encountered such a problem during development A...

Use vue3 to implement a human-cat communication applet

Table of contents Preface Initialize the project ...

Detailed explanation of Apache SkyWalking alarm configuration guide

Apache SkyWalking Apache SkyWalking is an applica...

CSS overflow-wrap new property value anywhere usage

1. First, understand the overflow-wrap attribute ...

Design reference WordPress website building success case

Each of these 16 sites is worth reading carefully,...

HTML Language Encyclopedia

123WORDPRESS.COM--HTML超文本标记语言速查手册<!-- --> !D...