🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

keep-alive-vue3

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

keep-alive-vue3

Extend vue3 keep-alive and router-view, add the function of automatically judging whether to use the cache. Support for vue2 [Click here](https://www.npmjs.com/package/keep-alive-vue2)

1.0.12
Source
npm
Version published
Weekly downloads
7
-41.67%
Maintainers
1
Weekly downloads
 
Created
Source

keep-alive-vue3 中文

Extend vue3 keep-alive and router-view, add the function of automatically judging whether to use the cache. Support for vue2 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-vue3 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-vue3

Steps for usage

First: import and register component

import KeepAliveVue3 from 'keep-alive-vue3';

Vue.use(KeepAliveVue3);

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

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

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

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

Example1
<-- Recommend -->
<keep-alive-vue3 :cache="$route.meta.cache" />
Example2
<-- Use cache for items with tab manager -->
<keep-alive-vue3
    :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-vue3 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

The page displayed by the push 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

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

$router.replace

The page displayed by the 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

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

$router.back

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

this.$router.back({
  cache: false
});

$router.go

The page displayed by the go interface uses the cache function by default when it is less than 0, and the cache is prohibited by default when it is greater than 0. If not use cached page, configure cache to false Note that defaultCache can change the default cache

this.$router.go(-1, {
  cache: false
});

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

keep-alive-vue3 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.

Keywords

vue3

FAQs

Package last updated on 28 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts