Vue implements page caching function

Vue implements page caching function

This article example shares the specific code of Vue to implement the page caching function for your reference. The specific content is as follows

Keep-alive is mainly used to jump from the list page to the detail page, and then when you click back, the page cache does not need to request resources again.

1. Configure routing in the router

Define in meta whether the page needs to be cached

import Vue from "vue";
import Router from "vue-router";

// Avoid redundant navigation to the current location const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router);
export default new Router({
  base: '',
  routes: [{
      path: "/",
      name: "index",
      component: () => import("@/layout"),
      redirect: '/login',
      children: [
        {
          path: 'dutySheet',
          name: 'dutySheet',
          component: () => import("@/pages/Dashboard/DutySheet")
        },
        {
          path: 'searchWord',
          name: 'searchWord',
          component: () => import("@/pages/dailyReportManage/searchWord/index"),
          meta: {
            keepAlive: true // Need to cache the page}
        },
        // Matching maintenance {
          path: "troopAction",
          name: "troopAction",
          component: () => import("@/pages/Dashboard/TroopAction"),
          meta: {
            keepAlive: false // No cache required}
     },
      ]
    },
  ]
});

2. Configure APP.vue

Use keep-alive for caching

<keep-alive>
    <router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>

3. When you click the back button, just call the this.$router.back() method

// return bacKBnt(){
        this.$router.back()
      },

4. Clear the cache

Only jumps to the "exhibitionWord" or "exhibitionWeekWord" page are cached, and jumps to other pages do not need to be cached.

beforeRouteLeave(to, from, next) {
      if (to.name == 'exhibitionWord' || to.name == 'exhibitionWeekWord') { // Route name to be cached
          from.meta.keepAlive = true
          next()
        }else{
          from.meta.keepAlive = false
          next()
      }
    },

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to read and write data in local cache in Vue
  • Summary of Vue browser local storage issues
  • Detailed explanation of the principle of Vue monitoring data
  • Vue calls local cache mode (monitoring data changes)

<<:  How to install OpenSuse on virtualbox

>>:  Detailed explanation of the difference between MySQL normal index and unique index

Recommend

How to install vim editor in Linux (Ubuntu 18.04)

You can go to the Ubuntu official website to down...

Detailed explanation of CentOS configuration of Nginx official Yum source

I have been using the CentOS purchased by Alibaba...

Example of how to enable Slow query in MySQL

Preface Slow query log is a very important functi...

Understanding innerHTML

<br />Related articles: innerHTML HTML DOM i...

Design theory: people-oriented green design

Reflections on the two viewpoints of “people-orie...

How to use crontab to add scheduled tasks in Linux

Preface The Linux system is controlled by the sys...

How to install MySQL 8.0 database on M1 chip (picture and text)

1. Download First of all, I would like to recomme...

Implementation of a simple login page for WeChat applet (with source code)

Table of contents 1. Picture above 2. User does n...

Solve the problem of secure_file_priv null

Add secure_file_priv = ' '; then run cmd ...

Vue uses the method in the reference library with source code

The official source code of monaco-editor-vue is ...