Home > Web Front-end > JS Tutorial > body text

Implementation of vue's keep-alive cache function

亚连
Release: 2018-05-26 16:30:07
Original
1883 people have browsed it

This article mainly introduces the implementation of vue's keep-alive cache function. Now I share it with you and give it as a reference.

Vue implements caching of component information

When we develop a vue project, it is inevitable that the component data will be lost after the route is switched to other components and then returned. Reloading, to handle this situation we need to use keep-alive to cache vue's component information so that it will not be reloaded.

1. In app.vue

<keep-alive>
  <router-view></router-view>
</keep-alive>
Copy after login

But this situation will cause Caching all components cannot achieve the effect of caching a single component.

Then we add some components and the implementation method is as follows:

In app.vue

<!--这里是需要keepalive的-->
<keep-alive>
  <router-view v-if="$route.meta.keepAlive"></router-view>
<keep-alive>

<!-- 这里不会被keepAlive -->

<router-view v-if="!$route.meta.keepAlive"></router-view>
Copy after login

##2. In the routing index.js page

{
  path: &#39;&#39;,
  name: &#39;&#39;,
  component: &#39;&#39;,
  meta: {keepAlive: true}   // 这个是需要keepalive的
},
{
  path: &#39;&#39;,
  name: &#39;&#39;,
  component: ,
  meta: {keepAlive: false}  // 这是不会被keepalive的
}
Copy after login

This realizes the caching function of some components

If the cached component wants to clear data or perform an initialization method, call the activated hook function when loading the component, as follows:

activated: function () {
  this.data = ‘&#39;
}
Copy after login

The above is me I compiled it for everyone, I hope it will be helpful to everyone in the future.

Related articles:

Very practical ajax user registration module

Ajax clicks to continuously load the data list (graphic tutorial )

Ajax Struts2 implements verification code verification function (graphic tutorial)

The above is the detailed content of Implementation of vue's keep-alive cache function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!