1.2.4 • Published 6 months ago

keep-alive-vue2 v1.2.4

Weekly downloads
-
License
-
Repository
github
Last release
6 months ago

keep-alive-vue2 中文

Extend vue2 keep-alive and router-view, add the function of automatically judging whether to use the cache.

You can use vite-app-pro cli to create template project,keep-alive-vue2 was built in project,convenient experience and use。

Support for vue3 Click here

The background of the problem

If the page uses keep-alive and router-view, the advantage is that the operation state of the previous step is quickly restored when the next page operation returns, and this experience is very good.

But it also brings problems.

When the user enters the page from the navigation menu or breadcrumb, a brand new page is needed, but the cached page is actually used, and this result is not what we want.

keep-alive-vue2 solves this problem.

It uses the cache when you operate $router.back and $router.go to return the page by default, and $router.push and $router.replace do not use the cache by default.

Install

npm i keep-alive-vue2

Steps for usage

First: import and register component

import KeepAliveVue2 from 'keep-alive-vue2';

Vue.use(KeepAliveVue2);

Second: use keep-alive-vue2 component replace keep-alive and router-view components

keep-alive-vue2 encapsulates keep-alive and router-view internally,

so you only need to write the keep-alive-vue2 component element.

The cache attribute is used to cache the use of page caching.

Example1
<-- Recommend -->
<keep-alive-vue2 :cache="$route.meta.cache" />
Example2
<-- Use cache for items with tab manager -->
<keep-alive-vue2
    :cache="!$route.meta || !$route.meta.noCache"
    :defaultCache="true" />

Third: must use the method of the vue-router instance. Only after $router.go and $router.back are called, the cached page is used.

keep-alive-vue2 properties descriptions

propertydescriptiontypeoptiondefault
cachewhether to cache pageBooleantrue/falsefalse
defaultCache$router.push、$router.replace and $router.go(value is greater than 0) parameter cache will use the valueBooleantrue/falsefalse
namerouter-view nameString--
includeonly components with matching names will be cachedRegExp--
excludeany component whose name matches will not be cachedRegExp--
maxmaximum number of component instances that can be cachedNumber--

vue-router interface extensions

$router.push/replace

The page displayed by the push/replace interface does not use the cache function by default. If you need to use it, configure cache to true Note that defaultCache can change the default cache

// disable cache
this.$router.push({
  name: 'list',
});
this.$router.replace({
  name: 'list',
});

// use cache
this.$router.push({
  name: 'list',
  cache: true
});
this.$router.replace({
  name: 'list',
  cache: true
});

$router.back/forward/go

The page displayed by the back/forward/go interface uses the cache function by default. If not use cached page, configure cache to false

// defaut use cache
this.$router.back();
this.$router.forward();
this.$router.go(1);

// disable cache
this.$router.back({cache: false});
this.$router.forward({cache: false});
this.$router.go(1, {cache: false});

keep-alive-vue2 attribute cache and $router interface parameter cache values determine whether the page uses cache.

keep-alive-vue2 cache$router cacheWhether to use cache
truetrueYes
truefalseNot
falsetrueNot
falsefalseNot

The page cache takes effect when both cache values are true. None of the others use cached pages.

1.2.4

6 months ago

1.2.3

6 months ago

1.2.2

6 months ago

1.2.1

11 months ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago