Detailed explanation of vue keepAlive cache clearing problem case

Detailed explanation of vue keepAlive cache clearing problem case

Keepalive is often used for caching in Vue projects, which is very convenient for meeting basic requirements. However, it is a bit troublesome to cache or not cache the same page according to different conditions.

First, list the pits:

1.

<keep-alive v-if="xxx">
          <router-view />
</keep-alive>
<keep-alive v-else>
          <router-view />
</keep-alive>

There are many methods like this on the Internet. When using this method, the cached items cannot be deleted. In fact, this method divides the cached pages and non-cached pages into two components for display. Although it looks like that in general, it actually displays different components at different times based on your conditions.

2.

vm.$destroy()

When thinking about removing the existing cache, I guess most people's first reaction is to think about how to delete the cache, so I also tried to find a way to delete the cache. Then the destroy method of vue is called. When it is destroyed, you will be very happy to find that it has been realized! The cache is deleted ~ So you think it's fixed and go to develop something else. Suddenly one day you realize, eh? Why is my page not cached? After some investigation, it was found that pages that called $destroy() would no longer be cached. .

Finally my solution:

Template
<keep-alive :include="keepAlive.join(',')">
         <router-view />
</keep-alive>

vuex

keepAlive: [
        '/joinManage/register/add-step1',
        '/joinManage/register/add-step2',
        '/joinManage/register/add-step3',
        '/joinManage/config/add-step1',
        '/joinManage/config/add-step2',
        '/joinManage/config/add-step3',
        '/joinManage/config/add-step4',
        '/joinManage/config/add-step5',
    ],

Use include+vuex to dynamically change the required cache pages. Include accepts the name of the component (I find naming troublesome here, so I just use the path to name it, but it doesn't actually use the path)

In this way, when we need to cache a certain page, we add its name to the keepalive array, and delete the corresponding one if it is not needed. This will achieve the cache deletion effect of keepAlive

This is the end of this article about the detailed case of vue keepAlive cache clearing problem. For more relevant vue keepAlive cache clearing problem 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:
  • Instructions for using keepAlive in Vue projects (super practical version)
  • Detailed explanation of keepAlive usage in Vue front-end development
  • Detailed explanation of keepAlive use cases in Vue
  • Detailed explanation of the function and usage of keepAlive component in Vue
  • Solve the problem of not caching when using keepAlive in Vue
  • Vue keepsAlive setting does not take effect and its solution

<<:  MySQL 8.0 Window Function Introduction and Summary

>>:  Three commonly used MySQL data types

Recommend

Steps to build the vite+vue3+element-plus project

Use vite to build a vue3 project You can quickly ...

How to implement https with nginx and openssl

If the server data is not encrypted and authentic...

js to implement add and delete table operations

This article example shares the specific code of ...

MySQL conditional query and or usage and priority example analysis

This article uses examples to illustrate the usag...

How to deploy MySQL and Redis services using Docker

Table of contents How to deploy MySQL service usi...

Detailed explanation of mysql deadlock checking and deadlock removal examples

1. Query process show processlist 2. Query the co...

Detailed explanation of TypeScript's basic types

Table of contents Boolean Type Number Types Strin...

Make your website automatically use IE7 compatibility mode when browsing IE8

Preface To help ensure that your web pages have a ...

Tutorial on installing mongodb under linux

MongoDB is cross-platform and can be installed on...

Example of how to deploy Spring Boot using Docker

Here we mainly use spring-boot out of the box, wh...

Pros and Cons of Vite and Vue CLI

There is a new build tool in the Vue ecosystem ca...

Example of using Docker Swarm to build a distributed crawler cluster

During the crawler development process, you must ...