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

The benefits and examples of placing the site map at the bottom of the web page

In the past, almost every website had a sitemap p...

Getting Started Tutorial for Beginners⑧: Easily Create an Article Site

In my last post I talked about how to make a web p...

Tomcat server security settings method

Tomcat is an HTTP server that is the official ref...

JavaScript typing game

This article shares the specific code of JavaScri...

Using CSS3 to create header animation effects

Netease Kanyouxi official website (http://kanyoux...

Alibaba Cloud Ubuntu 16.04 builds IPSec service

Introduction to IPSec IPSec (Internet Protocol Se...

Detailed process of installing Presto and connecting Hive in Docker

1. Introduction Presto is an open source distribu...

Details on using bimface in vue

Table of contents 1. Install Vue scaffolding 2. C...

Nginx installation error solution

1. Unzip nginx-1.8.1.tar.gz 2. Unzip fastdfs-ngin...

Detailed explanation of the spacing problem between img tags

IMG tag basic analysis In HTML5, the img tag has ...

How to install a virtual machine with Windows services on Mac

1. Download the virtual machine Official download...

Detailed explanation of mysql.user user table in Mysql

MySQL is a multi-user managed database that can a...

How to use JS to parse the excel content in the clipboard

Table of contents Preface 1. Paste Events and Cli...

Summary of some efficient magic operators in JS

JavaScript now releases a new version every year,...