feat: add refresh route

This commit is contained in:
xiaoxian521
2021-04-19 19:22:24 +08:00
parent fdff569de9
commit f9ff87c708
5 changed files with 304 additions and 208 deletions

View File

@@ -1,85 +1,88 @@
import { reactive, toRefs, nextTick } from "vue"
import { storageLocal } from "/@/utils/storage"
import { useRouter } from "vue-router"
import { reactive, toRefs, nextTick } from "vue";
import { storageLocal } from "/@/utils/storage";
import { useRouter } from "vue-router";
interface InterDynamic {
dRoutes: object[],
[propName: string]: any
dRoutes: object[];
[propName: string]: any;
}
// 默认显示首页tag
let dynamic: InterDynamic = reactive({
dRoutes: [
{
path: "/welcome", meta: {
path: "/welcome",
meta: {
title: "home",
icon: 'el-icon-s-home',
icon: "el-icon-s-home",
showLink: true,
savedPosition: false,
}
}]
})
},
},
],
});
export function useDynamicRoutesHook() {
const router = useRouter()
const router = useRouter();
/**
* @param value string 当前menu对应的路由path
* @param parentPath string 当前路由中父级路由
*/
const dynamicRouteTags = (value: string, parentPath: string): void => {
const hasValue = dynamic.dRoutes.some((item: any) => {
return item.path === value
})
return item.path === value;
});
function concatPath(arr: object[], value: string, parentPath: string) {
if (!hasValue) {
arr.forEach((arrItem: any) => {
let pathConcat = parentPath + '/' + arrItem.path
let pathConcat = parentPath + "/" + arrItem.path;
if (arrItem.path === value || pathConcat === value) {
dynamic.dRoutes.push({ path: value, meta: arrItem.meta })
dynamic.dRoutes.push({ path: value, meta: arrItem.meta });
// console.log(dynamic.dRoutes)
} else {
if (arrItem.children && arrItem.children.length > 0) {
concatPath(arrItem.children, value, parentPath)
concatPath(arrItem.children, value, parentPath);
}
}
})
});
}
}
concatPath(router.options.routes, value, parentPath)
concatPath(router.options.routes, value, parentPath);
if (storageLocal.getItem("routesInStorage") && storageLocal.getItem("routesInStorage").length > 2) {
let lens = storageLocal.getItem("routesInStorage").length
let itemss = storageLocal.getItem("routesInStorage")[lens - 1]
dynamic.dRoutes.push({ path: itemss.path, meta: itemss.meta })
}
// if (storageLocal.getItem("routesInStorage") && storageLocal.getItem("routesInStorage").length > 2) {
// let lens = storageLocal.getItem("routesInStorage").length
// let itemss = storageLocal.getItem("routesInStorage")[lens - 1]
// dynamic.dRoutes.push({ path: itemss.path, meta: itemss.meta })
// }
storageLocal.setItem("routesInStorage", dynamic.dRoutes)
}
// storageLocal.setItem("routesInStorage", dynamic.dRoutes)
};
/**
* @param value any 当前删除tag路由
* @param current objct 当前激活路由对象
*/
const deleteDynamicTag = async (obj: any, current: object): Promise<any> => {
let valueIndex: number = dynamic.dRoutes.findIndex((item: any) => {
return item.path === obj.path
})
return item.path === obj.path;
});
// 从当前匹配到的路径中删除
await dynamic.dRoutes.splice(valueIndex, 1)
storageLocal.setItem("routesInStorage", dynamic.dRoutes)
if (current === obj.path) { // 如果删除当前激活tag就自动切换到最后一个tag
let newRoute: any = dynamic.dRoutes.slice(-1)
await dynamic.dRoutes.splice(valueIndex, 1);
// storageLocal.setItem("routesInStorage", dynamic.dRoutes)
if (current === obj.path) {
// 如果删除当前激活tag就自动切换到最后一个tag
let newRoute: any = dynamic.dRoutes.slice(-1);
nextTick(() => {
router.push({
path: newRoute[0].path
})
})
path: newRoute[0].path,
});
});
}
}
};
return {
...toRefs(dynamic),
dynamicRouteTags,
deleteDynamicTag
}
deleteDynamicTag,
};
}

View File

@@ -16,7 +16,7 @@
<!-- 右侧功能按钮 -->
<ul class="right-func">
<li>
<i class="el-icon-refresh-right"></i>
<i class="el-icon-refresh-right" @click="onFresh"></i>
</li>
<li>
<i class="el-icon-arrow-down"></i>
@@ -58,6 +58,7 @@ import { useEventListener, useFullscreen } from "@vueuse/core";
import { toggleClass } from "/@/utils/operate";
let hiddenMainContainer = "hidden-main-container";
import options from "/@/settings";
import { useRouter, useRoute } from "vue-router";
interface setInter {
sidebar: any;
@@ -78,6 +79,9 @@ export default {
setup() {
const store = useStore();
const router = useRouter();
const route = useRoute();
const WIDTH = ref(992);
let containerHiddenSideBar = ref(options.hiddenSideBar);
@@ -149,6 +153,13 @@ export default {
}
}
function onFresh() {
const { path, fullPath } = unref(route);
router.replace({
path: "/redirect" + fullPath
});
}
onMounted(() => {
const isMobile = $_isMobile();
if (isMobile) {
@@ -170,7 +181,8 @@ export default {
...toRefs(set),
handleClickOutside,
containerHiddenSideBar,
onFullScreen
onFullScreen,
onFresh
};
}
};