Skip to content

Commit

Permalink
feat: 加上keepAlive全局开启配置
Browse files Browse the repository at this point in the history
  • Loading branch information
yuntian001 committed Sep 7, 2022
1 parent 54ed748 commit 091f9ad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ import * as localeConfig from './locale';
import loginConfig from './login';
import themeConfig from './theme';
const settingKey = 'me-config';
export { loginConfig, localeConfig, themeConfig, settingKey };
const settingConfig = {
openKeepAlive: true, // 是否开启KeepAlive缓存
};
export { loginConfig, localeConfig, themeConfig, settingKey, settingConfig };
35 changes: 19 additions & 16 deletions src/layout/components/page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,28 @@

<script setup lang="ts" name="LayoutPage">
import { MeKeepAliveProps } from '@/components/meKeepAlive';
import { settingConfig } from '@/config';
import { useRouteStore } from '@/store';
import { PropType, TransitionProps } from 'vue';
import { ComputedRef, PropType, TransitionProps } from 'vue';
const props = defineProps({
transition: Object as PropType<TransitionProps>,
});
const routeStore = useRouteStore();
const keepAliveProps = computed<MeKeepAliveProps>(() => ({
max: 30,
includeKey: [...routeStore.cacheFullPath],
}));
const route = useRoute();
watch(
route,
() => {
if (!route.meta.noCache) {
routeStore.cacheFullPath.add(route.fullPath);
}
},
{ immediate: true },
);
let keepAliveProps: undefined | ComputedRef<MeKeepAliveProps>;
if (settingConfig.openKeepAlive) {
keepAliveProps = computed<MeKeepAliveProps>(() => ({
max: 30,
includeKey: [...routeStore.cacheFullPath],
}));
const route = useRoute();
watch(
route,
() => {
if (!route.meta.noCache) {
routeStore.cacheFullPath.add(route.fullPath);
}
},
{ immediate: true },
);
}
</script>
2 changes: 1 addition & 1 deletion src/store/modules/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { mixColor } from '@/utils/helper';
const { css } = useStyleTag('');
const useSettingStore = defineStore('setting', {
state: () => ({
themeConfig: useStorage(`${settingKey}-theme`, themeConfig),
themeConfig: useStorage(`${settingKey}-theme`, themeConfig, localStorage, { mergeDefaults: true }),
locale: useStorage(`${settingKey}-locale`, localeConfig.localeSetting.locale ?? 'zh-cn'),
elLocale: undefined as Language | undefined,
isDark: useDark({ storageKey: 'me-color-dark-scheme' }),
Expand Down
7 changes: 7 additions & 0 deletions src/views/dashboard/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@
</template>
<script setup lang="ts" name="DashboardIndex">
const value = ref('');
const fullPath = useRoute().fullPath;
onActivated(() => {
console.log('active', fullPath);
});
onDeactivated(() => {
console.log('deactivated', fullPath);
});
</script>

0 comments on commit 091f9ad

Please sign in to comment.