Skip to content

Commit

Permalink
refactor(use-router): 重构useRouter的vue实例初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Aug 2, 2020
1 parent 9210541 commit 6d3afd2
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 48 deletions.
24 changes: 15 additions & 9 deletions src/hooks/core/useRouter.ts
Original file line number Diff line number Diff line change
@@ -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 };
};

/**
Expand All @@ -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);
};
4 changes: 2 additions & 2 deletions src/layouts/default/LayoutMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
default: '',
} as PropOptions<string>,
},
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 () => {
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/default/multitabs/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ 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';

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
Expand Down
2 changes: 2 additions & 0 deletions src/setup/main/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -27,6 +28,7 @@
export default defineComponent({
name: 'App',
setup(_, { root }) {
setupInitRumTimeVm();
// 检测网络状态
useNetWork({
onLineFn: () => {
Expand Down
25 changes: 0 additions & 25 deletions src/setup/vue/runtimeVm.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/setup/vue/setRunTimeVue.ts

This file was deleted.

0 comments on commit 6d3afd2

Please sign in to comment.