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

How to use keep-alive in vue2

亚连
Release: 2018-06-19 14:47:56
Original
2332 people have browsed it

vue2.0 provides a keep-alive component to cache components to avoid loading corresponding components multiple times and reduce performance consumption. This article introduces to you a summary and precautions for the use of keep-alive in vue2. Friends who need it can refer to it

keep-alive is a built-in component of Vue, which can retain the state in the memory during the component switching process. Prevent repeated rendering of the DOM. Combined with vue-router, the entire content of a view can be cached.

The basic usage is as follows:


 
 
 
Copy after login

Generally there is such a demand. When we enter the list page for the first time, we need to request data. When I enter the details page from the list page, the details page is not cached. You need to request data and then return to the list page

There are two situations:

1. Directly click the browser's back button.

2. Click the /list link in the navigation bar to return.

So in the first case, when we directly use the back button to return to the list page (/list), there is no need to request data.

In the second case, we need to request data by returning to the list page through the link.

So there are three situations here:

#1. By default, data needs to be requested when entering the list page.

2. After entering the details page, use the browser's default back button to return, which does not require an ajax request.

3. After entering the details page and returning to the list page by clicking the link, you also need to send an ajax request.

The configuration is as follows:

1. The configuration of the entry file app.vue is as follows:



 

Copy after login

2. Set the meta attribute in the router and set keepAlive: true to indicate the need to use cache , if false, it means no need to use cache. And add scroll behavior scrollBehavior

router/index.js configuration is as follows:

import Vue from 'vue';
import Router from 'vue-router';
// import HelloWorld from '@/views/HelloWorld';
Vue.use(Router);
const router = new Router({
 mode: 'history', // 访问路径不带井号 需要使用 history模式,才能使用 scrollBehavior
 base: '/page/app', // 配置单页应用的基路径
 routes: [
 {
  path: '/',
  name: 'list',
  component: resolve => require(['@/views/list'], resolve), // 使用懒加载
  meta: {
  keepAlive: true // true 表示需要使用缓存
  }
 },
 {
  path: '/list',
  name: 'list',
  component: resolve => require(['@/views/list'], resolve), // 使用懒加载
  meta: {
  keepAlive: true // true 表示需要使用缓存 false表示不需要被缓存
  }
 },
 {
  path: '/detail',
  name: 'detail',
  component: resolve => require(['@/views/detail'], resolve) // 使用懒加载
 }
 ],
 scrollBehavior (to, from, savedPosition) {
 // 保存到 meta 中,备用
 to.meta.savedPosition = savedPosition;
 if (savedPosition) {
  return { x: 0, y: 0 };
 }
 return {};
 }
});
export default router;
Copy after login

3. list.vue code is as follows:



Copy after login

4. detail.vue code is as follows:


Copy after login

Two: Use router.meta extension

Assume there are 3 pages now, the requirements are as follows:

1. There is page A by default, and a request is required for page A to come in.

2. Page B jumps to page A, and page A does not need to be requested again.

3. Page C jumps to page A, and page A needs to be requested again.

The implementation method is as follows:

Set the meta attribute in route A:

{
 path: '/a',
 name: 'A',
 component: resolve => require(['@/views/a'], resolve),
 meta: {
 keepAlive: true // true 表示需要使用缓存
 }
}
Copy after login

So all the codes under router/index become as follows:

import Vue from 'vue';
import Router from 'vue-router';
// import HelloWorld from '@/views/HelloWorld';
Copy after login

Vue.use(Router);

const router = new Router({
 mode: 'history', // 访问路径不带井号 需要使用 history模式,才能使用 scrollBehavior
 base: '/page/app', // 配置单页应用的基路径
 routes: [
 {
  path: '/',
  name: 'list',
  component: resolve => require(['@/views/list'], resolve), // 使用懒加载
  meta: {
  keepAlive: true // true 表示需要使用缓存
  }
 },
 {
  path: '/list',
  name: 'list',
  component: resolve => require(['@/views/list'], resolve), // 使用懒加载
  meta: {
  keepAlive: true // true 表示需要使用缓存 false表示不需要被缓存
  }
 },
 {
  path: '/detail',
  name: 'detail',
  component: resolve => require(['@/views/detail'], resolve) // 使用懒加载
 },
 {
  path: '/a',
  name: 'A',
  component: resolve => require(['@/views/a'], resolve),
  meta: {
  keepAlive: true // true 表示需要使用缓存
  }
 },
 {
  path: '/b',
  name: 'B',
  component: resolve => require(['@/views/b'], resolve)
 },
 {
  path: '/c',
  name: 'C',
  component: resolve => require(['@/views/c'], resolve)
 }
 ],
 scrollBehavior (to, from, savedPosition) {
 // 保存到 meta 中,备用
 to.meta.savedPosition = savedPosition;
 if (savedPosition) {
  return { x: 0, y: 0 };
 }
 return {};
 }
});
export default router;
Copy after login

Set beforeRouteLeave in component B

beforeRouteLeave(to, from, next) {
 // 设置下一个路由meta
 to.meta.keepAlive = true; // 让A缓存,不请求数据
 next(); // 跳转到A页面
}
Copy after login

All the codes of component B are as follows:



Copy after login

Set beforeRouteLeave in component C:

All the codes in the
beforeRouteLeave(to, from, next) {
 // 设置下一个路由meta
 to.meta.keepAlive = false; // 让A不缓存,重新请求数据
 console.log(to)
 next(); // 跳转到A页面
}
Copy after login

c component are as follows:



Copy after login

All the codes in the a component are as follows:



Copy after login

Note that the b component does not re-request data from the a component (including clicked links and browsers) Back button), the c component requests data from the a component (including clicked links and browser back buttons).

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

Related articles:

Using Koa to build projects through Node.js

Is using JavaScript a better solution than asynchronous implementation?

About the use of Vue high-order components

Detailed introduction to Vue data binding

About Website generation chapter directory code example

How to use treeview to dynamically load data in the Bootstrap framework

The above is the detailed content of How to use keep-alive in vue2. 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!