Skip to content

Commit

Permalink
fix: 已登录情况下访问时loading无法消除问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
yuntian001 committed Oct 19, 2022
1 parent 674d849 commit b928655
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<el-config-provider :locale="settingStore.elLocale" :size="settingStore.themeConfig.size">
<router-view v-slot="{ Component }">
<me-component :is="Component" done-progress close-loading></me-component>
<me-component :is="Component" done-progress close-loading="layout"></me-component>
</router-view>
</el-config-provider>
</template>
Expand Down
6 changes: 3 additions & 3 deletions src/components/meComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MeKeepAliveProps, default as MeKeepAlive } from './meKeepAlive';
import { useLoadMessages } from '@/locales/i18n';
import { done } from '@/utils/nProgress';
import { localeConfig } from '@/config';
import { closeLoading } from '@/utils/loading';
import { closeLoading, loadingObject } from '@/utils/loading';
export default defineComponent({
name: 'MeComponent',
props: {
Expand All @@ -13,7 +13,7 @@ export default defineComponent({
keepAlive: Object as PropType<MeKeepAliveProps>,
componentKey: [Number, String, Symbol],
doneProgress: Boolean,
closeLoading: Boolean,
closeLoading: String as PropType<keyof typeof loadingObject>,
transition: Object as PropType<TransitionProps>,
},
setup(props, { attrs }) {
Expand All @@ -30,7 +30,7 @@ export default defineComponent({
key.value = props.componentKey;
_attrs.value = attrs;
props.doneProgress && done();
props.closeLoading && closeLoading();
props.closeLoading && closeLoading(false,1,props.closeLoading);
}
},
{ immediate: true },
Expand Down
2 changes: 1 addition & 1 deletion src/layout/components/page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:keep-alive="keepAliveProps"
:component-key="route.fullPath"
done-progress
close-loading
close-loading="layout"
>
</me-component>
</router-view>
Expand Down
2 changes: 1 addition & 1 deletion src/router/guard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function createPermissionGuard(router: Router) {
function createProgressGuard(router: Router) {
router.beforeEach(async (to) => {
start(to.matched.length);
loading({}, to.matched.length);
loading({}, to.matched.length,'layout');
return true;
});
}
Expand Down
38 changes: 18 additions & 20 deletions src/utils/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useGlobalStore } from '@/store';
import { LoadingOptions } from 'element-plus';
import { throttle } from 'lodash-es';
let loadingInstance: ReturnType<typeof ElLoading.service>;
export type LoadingMode = 'auto'|'global'|'page'
class Loading {
constructor(private execLoading: (options?: LoadingOptions) => void, private execClose: () => void) {
this.execLoading = execLoading;
Expand Down Expand Up @@ -35,30 +34,29 @@ class Loading {
this.throttleClose.cancel();
}
}
export const globalLoading = new Loading(
(options?: LoadingOptions) => (loadingInstance = ElLoading.service(options)),
() => loadingInstance.close(),
);
export const layoutLoading = new Loading(
(options?: LoadingOptions) => (useGlobalStore().loadingOptions = options ? reactive(options) : {}),
() => (useGlobalStore().loadingOptions = undefined),
);
function getLoading(mode:LoadingMode) {
if (mode === 'page' || ( mode ==='auto' && useGlobalStore().layoutLoaded)) {
return layoutLoading;
}
return globalLoading;
}

export function loading(options?: LoadingOptions, number = 1, mode:LoadingMode = 'auto'): void {
getLoading(mode).loading(options, number);
export const loadingObject = {
global: new Loading(
(options?: LoadingOptions) => (loadingInstance = ElLoading.service(options)),
() => loadingInstance.close(),
),
layout:new Loading(
(options?: LoadingOptions) => (useGlobalStore().loadingOptions = options ? reactive(options) : {}),
() => (useGlobalStore().loadingOptions = undefined),
),
}

export function loading(options?: LoadingOptions, number = 1, mode:keyof typeof loadingObject = 'global') {
const loading = loadingObject[mode]
loading.loading(options, number);
return loading;
}

export function closeLoading(force = false, number = 1, mode:LoadingMode = 'auto'): void {
export function closeLoading(force = false, number = 1, mode:keyof typeof loadingObject = 'global'): void {
const loading = loadingObject[mode]
if (force) {
getLoading(mode).forceClose();
loading.forceClose();
return;
}
getLoading(mode).close(number);
loading.close(number);
}

0 comments on commit b928655

Please sign in to comment.