diff --git a/src/hooks/core/useRouter.ts b/src/hooks/core/useRouter.ts index 2f4a3441eac..669e6e122e8 100644 --- a/src/hooks/core/useRouter.ts +++ b/src/hooks/core/useRouter.ts @@ -1,11 +1,19 @@ -import { unref, computed } from 'compatible-vue'; +import { unref, computed, getCurrentInstance, Vue } from 'compatible-vue'; import { pageEnum } from '@/enums/pageEnum'; -import { getRuntimeVM } from '@/setup/vue/runtimeVm'; -import VurRouter from 'vue-router'; +// import { getRuntimeVM } from '@/setup/vue/runtimeVm'; + +let runtimeVm: Vue | null = null; + +export function setupInitRumTimeVm() { + const currentInstance = getCurrentInstance(); + if (currentInstance && !runtimeVm) { + runtimeVm = currentInstance; + } +} + export const useRouter = () => { - const vm = getRuntimeVM(); - const route = computed(() => vm.$route); - return { routeRef: route, router: vm.$router }; + const route = computed(() => runtimeVm!.$route); + return { routeRef: route, router: runtimeVm!.$router }; }; /** @@ -24,12 +32,10 @@ export const useRedo = () => { export const useGo = ({ path = pageEnum.BASE_HOME, replace = true, - router: rootRouter, }: { - router?: VurRouter; path?: pageEnum; replace?: boolean; } = {}) => { const { router } = useRouter(); - replace ? (rootRouter || router).replace(path) : (rootRouter || router).push(path); + replace ? router.replace(path) : router.push(path); }; diff --git a/src/layouts/default/LayoutMenu.vue b/src/layouts/default/LayoutMenu.vue index 2e39bb079c1..968adbcf2a8 100644 --- a/src/layouts/default/LayoutMenu.vue +++ b/src/layouts/default/LayoutMenu.vue @@ -26,14 +26,14 @@ default: '', } as PropOptions, }, - setup(props, { root }) { + setup(props) { const { prefixCls } = useDesign('layout-menu'); function handleMenuClick({ targetRoute }: { menu?: MenuItem; targetRoute: RouteConfigEx }) { const { path } = targetRoute; // const { path, meta: { frameSrc } = {} } = targetRoute; if (path) { - useGo({ path: path as pageEnum, replace: false, router: root.$router }); + useGo({ path: path as pageEnum, replace: false }); } } return () => { diff --git a/src/layouts/default/multitabs/index.vue b/src/layouts/default/multitabs/index.vue index 88058535fad..ec63f72d1e0 100644 --- a/src/layouts/default/multitabs/index.vue +++ b/src/layouts/default/multitabs/index.vue @@ -49,7 +49,7 @@ // tab切换 function handleChange(activeKey: pageEnum) { activeKeyRef.value = activeKey; - useGo({ path: activeKey, replace: false, router: root.$router }); + useGo({ path: activeKey, replace: false }); } // 关闭当前ab function handleEdit(targetKey: string) { diff --git a/src/setup/index.ts b/src/setup/index.ts index 1bfda54ace5..acc1192c256 100644 --- a/src/setup/index.ts +++ b/src/setup/index.ts @@ -6,7 +6,7 @@ import Scrollbar from '@/components/scrollbar'; import '@/setup/ant-design-vue/index'; import '@/setup/ant-design-vue/spin'; -import RunTimeVue from '@/setup/vue/setRunTimeVue'; +// import RunTimeVue from '@/setup/vue/setRunTimeVue'; import './freeze/freezeWindowConfig'; @@ -14,7 +14,7 @@ import { registerScriptErrorHandler } from '@/common/plugins/error-handle/index' export default { install: (Vue: VueConstructor): void => { Vue.use(VueCompositionAPI); - Vue.use(RunTimeVue); + // Vue.use(RunTimeVue); Vue.use(Scrollbar); // 使用svg sprit diff --git a/src/setup/main/App.vue b/src/setup/main/App.vue index 30835ebc64a..eb712034c4d 100644 --- a/src/setup/main/App.vue +++ b/src/setup/main/App.vue @@ -12,6 +12,7 @@ // hook import { useNetWork } from '@/hooks/event/useNetWork'; + import { setupInitRumTimeVm } from '@/hooks/core/useRouter'; import { useInitProjCfg } from './useInitApp'; import { appStore } from '@/store/modules/app'; @@ -27,6 +28,7 @@ export default defineComponent({ name: 'App', setup(_, { root }) { + setupInitRumTimeVm(); // 检测网络状态 useNetWork({ onLineFn: () => { diff --git a/src/setup/vue/runtimeVm.ts b/src/setup/vue/runtimeVm.ts deleted file mode 100644 index ae554c7742c..00000000000 --- a/src/setup/vue/runtimeVm.ts +++ /dev/null @@ -1,25 +0,0 @@ -interface Runtime { - vm?: Vue; -} - -const runtime: Runtime = {}; - -/** - * @description: 获取vue实例 - */ -export function getRuntimeVM(): Vue { - if (runtime.vm) { - return runtime.vm; - } - throw new ReferenceError('Not found vue instance.'); -} - -/** - * @description: 设置vue实例 - */ -export function setRuntimeVM(this: Vue, vue?: Vue) { - const vm = this || vue; - if (typeof vm.$options.setup === 'function') { - runtime.vm = vm; - } -} diff --git a/src/setup/vue/setRunTimeVue.ts b/src/setup/vue/setRunTimeVue.ts deleted file mode 100644 index e2c2e837b86..00000000000 --- a/src/setup/vue/setRunTimeVue.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { VueConstructor } from 'compatible-vue'; - -import { setRuntimeVM } from './runtimeVm'; - -export default { - install(Vue: VueConstructor) { - Vue.mixin({ beforeCreate: setRuntimeVM }); - }, -};