feat: add keepalive

This commit is contained in:
xiaoxian521 2021-08-29 10:20:59 +08:00
parent 860433bb22
commit 8187dbff0e
6 changed files with 90 additions and 48 deletions

View File

@ -1,12 +1,16 @@
{ {
"version": "1.0.0", "version": "1.0.0",
"keepAlive": true,
"MapConfigure": { "MapConfigure": {
"amapKey": "97b3248d1553172e81f168cf94ea667e", "amapKey": "97b3248d1553172e81f168cf94ea667e",
"baiduKey": "wTHbkkEweiFqZLKunMIjcrb2RcqNXkhc", "baiduKey": "wTHbkkEweiFqZLKunMIjcrb2RcqNXkhc",
"options": { "options": {
"resizeEnable": true, "resizeEnable": true,
"center": [113.6401, 34.72468], "center": [
113.6401,
34.72468
],
"zoom": 12 "zoom": 12
} }
} }
} }

View File

@ -5,9 +5,9 @@
</template> </template>
<script> <script>
import { ElConfigProvider } from "element-plus"; import { ElConfigProvider } from "element-plus"
import zhCn from "element-plus/lib/locale/lang/zh-cn"; import zhCn from "element-plus/lib/locale/lang/zh-cn"
import en from "element-plus/lib/locale/lang/en"; import en from "element-plus/lib/locale/lang/en"
export default { export default {
components: { components: {
[ElConfigProvider.name]: ElConfigProvider [ElConfigProvider.name]: ElConfigProvider
@ -17,9 +17,9 @@ export default {
currentLocale() { currentLocale() {
switch (this.$storage.locale?.locale) { switch (this.$storage.locale?.locale) {
case "zh": case "zh":
return zhCn; return zhCn
case "en": case "en":
return en; return en
} }
} }
} }

View File

@ -1,25 +1,47 @@
<template> <template>
<section class="app-main"> <section class="app-main">
<router-view :key="key" v-slot="{ Component }"> <router-view>
<transition appear name="fade-transform" mode="out-in"> <template #default="{ Component, route }">
<keep-alive> <transition appear name="fade-transform" mode="out-in">
<component :is="Component" /> <keep-alive v-if="keepAlive" :include="getCachedPageList">
</keep-alive> <component :is="Component" :key="route.fullPath" />
</transition> </keep-alive>
<component v-else :is="Component" :key="route.fullPath" />
</transition>
</template>
</router-view> </router-view>
</section> </section>
</template> </template>
<script lang="ts"> <script lang="ts">
import { computed, defineComponent } from "vue"; import {
ref,
unref,
computed,
defineComponent,
onBeforeMount,
getCurrentInstance
} from "vue";
import { useRoute } from "vue-router"; import { useRoute } from "vue-router";
import { useSettingStoreHook } from "/@/store/modules/settings";
export default defineComponent({ export default defineComponent({
name: "AppMain", name: "AppMain",
setup() { setup() {
let vm: any;
const keepAlive: Boolean = ref(
getCurrentInstance().appContext.config.globalProperties.$config?.keepAlive
);
const route = useRoute(); const route = useRoute();
const key = computed(() => route.path); const key = computed(() => route.path);
return { key }; const getCachedPageList = computed((): string[] => {
if (!unref(keepAlive)) {
return [];
}
return useSettingStoreHook().cachedPageList;
});
return { key, keepAlive, getCachedPageList };
} }
}); });
</script> </script>

View File

@ -5,13 +5,16 @@ import { store } from "/@/store";
interface SettingState { interface SettingState {
title: string; title: string;
fixedHeader: boolean; fixedHeader: boolean;
cachedPageList: string[];
} }
export const useSettingStore = defineStore({ export const useSettingStore = defineStore({
id: "pure-setting", id: "pure-setting",
state: (): SettingState => ({ state: (): SettingState => ({
title: defaultSettings.title, title: defaultSettings.title,
fixedHeader: defaultSettings.fixedHeader fixedHeader: defaultSettings.fixedHeader,
// 需要开启keepalive的页面数组里面放页面的name即可
cachedPageList: ["editor"]
}), }),
getters: { getters: {
getTitle() { getTitle() {

View File

@ -16,27 +16,39 @@
"baseUrl": ".", "baseUrl": ".",
"allowJs": false, "allowJs": false,
"resolveJsonModule": true, // json "resolveJsonModule": true, // json
"lib": ["dom", "esnext"], "lib": [
"dom",
"esnext"
],
"incremental": true, "incremental": true,
"paths": { "paths": {
"/@/*": ["src/*"], "/@/*": [
"/#/*": ["types/*"] "src/*"
],
"/#/*": [
"types/*"
]
}, },
"types": ["node", "vite/client"], "types": [
"typeRoots": ["./node_modules/@types/", "./types"] "node",
"vite/client"
],
"typeRoots": [
"./node_modules/@types/",
"./types"
]
}, },
"include": [ "include": [
"src/**/*.ts", "src/**/*.ts",
"src/**/*.tsx", "src/**/*.tsx",
"src/**/*.vue", "src/**/*.vue",
"tests/**/*.ts", "types/*.d.ts",
"src/utils/path.js", "mock/*.ts",
"types/**/*.d.ts", "vite.config.ts"
"types/**/*.ts",
"types/global.d.ts",
"types/shims-tsx.d.ts",
"types/shims-vue.d.ts",
"mock/asyncRoutes.ts"
], ],
"exclude": ["node_modules", "dist", "**/*.js"] "exclude": [
} "node_modules",
"dist",
"**/*.js"
]
}

View File

@ -1,29 +1,29 @@
import { resolve } from "path"; import { resolve } from "path"
import { UserConfigExport, ConfigEnv } from "vite"; import { UserConfigExport, ConfigEnv } from "vite"
import vue from "@vitejs/plugin-vue"; import vue from "@vitejs/plugin-vue"
import vueJsx from "@vitejs/plugin-vue-jsx"; import vueJsx from "@vitejs/plugin-vue-jsx"
import { loadEnv } from "./build/utils"; import { loadEnv } from "./build/utils"
import { createProxy } from "./build/proxy"; import { createProxy } from "./build/proxy"
import { viteMockServe } from "vite-plugin-mock"; import { viteMockServe } from "vite-plugin-mock"
import styleImport from "vite-plugin-style-import"; import styleImport from "vite-plugin-style-import"
import VitePluginElementPlus from "vite-plugin-element-plus"; import VitePluginElementPlus from "vite-plugin-element-plus"
const pathResolve = (dir: string): any => { const pathResolve = (dir: string): any => {
return resolve(__dirname, ".", dir); return resolve(__dirname, ".", dir)
}; }
const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = loadEnv(); const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = loadEnv()
const alias: Record<string, string> = { const alias: Record<string, string> = {
"/@": pathResolve("src"), "/@": pathResolve("src"),
//解决开发环境下的警告 You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle. //解决开发环境下的警告 You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.
"vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js" "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
}; }
const root: string = process.cwd(); const root: string = process.cwd()
export default ({ command }: ConfigEnv): UserConfigExport => { export default ({ command }: ConfigEnv): UserConfigExport => {
const prodMock = true; const prodMock = true
return { return {
/** /**
* *
@ -84,6 +84,7 @@ export default ({ command }: ConfigEnv): UserConfigExport => {
] ]
}, },
build: { build: {
// @ts-ignore
sourcemap: false, sourcemap: false,
brotliSize: false, brotliSize: false,
// 消除打包大小超过500kb警告 // 消除打包大小超过500kb警告
@ -92,5 +93,5 @@ export default ({ command }: ConfigEnv): UserConfigExport => {
define: { define: {
__INTLIFY_PROD_DEVTOOLS__: false __INTLIFY_PROD_DEVTOOLS__: false
} }
}; }
}; }