Home > Web Front-end > JS Tutorial > Vue built-in components: introduction and use of keep-alive components (with code)

Vue built-in components: introduction and use of keep-alive components (with code)

不言
Release: 2018-08-15 15:26:53
Original
1813 people have browsed it

The content of this article is about Vue’s built-in components: the introduction and use of keep-alive components (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

keep-alive Introduction

keep-alive is a built-in component of Vue that allows included components to retain state or avoid re-rendering.
After the vue 2.1.0 version, keep-alive has added two new attributes: include (included components are cached) and exclude (excluded components are not cached and have a higher priority than include).

Usage

<keep-alive include=&#39;include_components&#39; exclude=&#39;exclude_components&#39;>
  <component>
    <!-- 该组件是否缓存取决于include和exclude属性 -->
  </component>
</keep-alive>
Copy after login

Parameter explanation
include - string or regular expression, only components with matching names will be cached
exclude - String or regular expression, any component with a matching name will not be cached
The include and exclude attributes allow components to be cached conditionally. Both can be represented as a comma-separated string, a regular expression, or an array. When using regular expressions or arrays, be sure to use v-bind !

Usage examples

<!-- 逗号分隔字符串,只有组件a与b被缓存。 -->
<keep-alive include="a,b">
  <component></component>
</keep-alive>

<!-- 正则表达式 (需要使用 v-bind,符合匹配规则的都会被缓存) -->
<keep-alive :include="/a|b/">
  <component></component>
</keep-alive>

<!-- Array (需要使用 v-bind,被包含的都会被缓存) -->
<keep-alive :include="[&#39;a&#39;, &#39;b&#39;]">
  <component></component>
</keep-alive>
Copy after login

Usage scenarios

1. Although you can use regular expressions Expressions to include and exclude some components, but more commonly used are to explicitly specify whether the component should be cached through the router.meta attribute

router.meta configuration

...
    {
        path: 'edit',
        component: () => import('@/views/site/edit'),
        name: 'site.edit',
        meta: {
            title: '网址编辑',
            hidden: true,
            cache: false
        }
    },
    {
        path: 'list',
        component: () => import('@/views/site/list'),
        name: 'site.list',
        meta: {
            title: '网址列表',
            hidden: false,
            cache: true
        }
    },
...
Copy after login

Then use the v-if tag to determine whether caching is needed

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

<!-- 不缓存 -->
<router-view v-if="!$route.meta.cache"></router-view>
Copy after login

2. Different page switching has different refresh rules: B->A does not refresh, C-A refresh

Route A configuration

{
        path: '/',
        name: 'A',
        component: A,
        meta: {
            cache: true // 需要被缓存
        }
}
Copy after login

Component B configuration

export default {
        data() {
            return {};
        },
        methods: {},
        beforeRouteLeave(to, from, next) {
             // 设置下一个路由的 meta
            to.meta.cache = true;  // 让 A 缓存,即不刷新
            next();
        }
};
Copy after login

Component C configuration

export default {
        data() {
            return {};
        },
        methods: {},
        beforeRouteLeave(to, from, next) {
            // 设置下一个路由的 meta
            to.meta.cache = false; // 让 A 不缓存,即刷新
            next();
        }
};
Copy after login

Note

If the component is Cache, created() method will not be executed. Generally, we will request data and load the list in the created method. If the current page is cached and the background data is updated, the data will not be displayed in the foreground in time. At this time, the page needs to be refreshed manually.
So whether the component needs to be cached depends on things

export default {
        data() {
            return {};
        },

        created() {
            // do some thing...
        },
        methods: {},
};
Copy after login

Related recommendations:

vue Custom select built-in component

vue Summary of built-in directive usage

Vue component option props

The above is the detailed content of Vue built-in components: introduction and use of keep-alive components (with code). 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