KeepAlive
Note
This solution is only applicable to Vue 3.
Setup
- Install the plugin
bash
$ npm add @winner-fed/plugin-keepalive -D
bash
$ yarn add @winner-fed/plugin-keepalive -D
bash
$ pnpm add @winner-fed/plugin-keepalive -D
bash
$ bun add @winner-fed/plugin-keepalive -D
- Enable the plugin in the
.winrc
configuration file
ts
import { defineConfig } from 'win';
export default defineConfig({
plugins: ['@winner-fed/plugin-keepalive'],
routes: [{
path: '/',
name: 'Home',
component: 'index',
routes: [
{
path: 'admin',
name: 'Admin',
component: '@/components/admin'
},
{
path: 'normal',
name: 'Normal',
component: '@/components/normal'
}
]
}],
/**
* @name Route state persistence plugin
* @doc https://winjs-dev.github.io/winjs-docs/plugins/keepalive.html
*/
keepalive: ['/hello', '/docs'],
// Need to disable mfsu
mfsu: false
});
Introduction
Configure routes that require state persistence.
- Type:
(string | RegExp)[]
ts
import { defineConfig } from 'win';
export default defineConfig({
keepalive: ['/list'],
});
Note that the keepalive configuration supports regular expressions. However, all route regex matching should be lowercase. For example, regardless of whether your route is
home
,Home
, orhoMe
, only settingkeepalive:[/home/]
will be effective. String configuration works the opposite way - if your route ishome
, configuringhome
,Home
, orhoMe
will all be effective.
Wherever you need to use <router-view></router-view>
, you should replace it with <KeepAliveLayout>
diff
<template>
- <router-view></router-view>
+ <KeepAliveLayout></KeepAliveLayout>
</template>
+ <script lang="ts" setup>
+ import KeepAliveLayout from '@@/plugin-keepalive/layout.vue';
+ </script>