Home > Web Front-end > JS Tutorial > Detailed explanation of how to use keep-alive in vue

Detailed explanation of how to use keep-alive in vue

php中世界最好的语言
Release: 2018-05-29 14:32:11
Original
4376 people have browsed it

This time I will bring you a detailed explanation of how to use keep-alive in Vue. What are the precautions for using keep-alive in Vue? Here are practical cases, let’s take a look.

1. The role and benefits of keep-alive

In e-commerce related projects, when we enter the list page for the first time I need to request data. When I enter the details page from the list page, I need to request the data even if the details page is not cached, and then return to the list page. At this time, we use keep-alive to cache the component to prevent secondary rendering, which will save a lot of money. performance.

2. Basic usage of keep-alive

In app.vue

<!-- 缓存所有的页面 -->
<keep-alive>
 <router-view v-if="$route.meta.keep_alive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keep_alive"></router-view>
Copy after login

The component content that needs to be cached is directly Add in the router:

meta: {
 keepAlive: true // true 表示需要使用缓存 false表示不需要被缓存
 }
Copy after login

3.keep-alive life cycle

When keep-alive is introduced, the page first Upon entry, the trigger sequence of the hook is created-> mounted-> activated, and deactivated is triggered when exiting. When entering again (forward or backward), only activated is triggered.

Let’s take a look at the usage problems and solutions of keep-alive in vue

Problem description

In business development, there will be scenarios where routing jumps but data needs to be retained when returning; vue provides keep-alive to handle

Solution

Return The dom does not allow it to be refreshed, and wraps a layer outside the vue-view. When keep-alive is introduced, the page is entered for the first time, the trigger sequence of the hook is created-> mounted-> activated, and deactivated is triggered when exiting. When entering again (forward or backward), only activated is triggered.

Event mounting methods, etc., which are only executed once are placed in mounted; methods that are executed each time the component is entered are placed in activated;
Whether to wrap keep-alive can be configured through parameters;

<keep-alive>
  <router-view v-if="$route.meta.keepAlive" style="min-height:100%"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive" style="min-height:100%"></router-view>
//不需要刷新的路由配置里面配置 meta: {keepAlive: true}, 这个路由则显示在上面标签;
//需要刷新的路由配置里面配置 meta: {keepAlive: false}, 这个路由则显示在下面标签;
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use element-ui to restrict date selection

How to completely uninstall node and npm on Mac

The above is the detailed content of Detailed explanation of how to use keep-alive in vue. 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