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

View File

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

View File

@ -1,118 +1,153 @@
import { createRouter, createWebHistory, createWebHashHistory, RouteRecordRaw } from "vue-router" import {
createRouter,
createWebHistory,
createWebHashHistory,
RouteRecordRaw,
} from "vue-router";
import Layout from '../layout/index.vue' import Layout from "../layout/index.vue";
import { storageSession } from "../utils/storage" import { storageSession } from "../utils/storage";
const routes: Array<RouteRecordRaw> = [ const routes: Array<RouteRecordRaw> = [
{ {
path: '/', path: "/",
name: 'home', name: "home",
component: Layout, component: Layout,
redirect: "/welcome", redirect: "/welcome",
children: [{
path: '/welcome',
name: 'welcome',
component: () => import(/* webpackChunkName: "home" */ '../views/welcome.vue'),
meta: {
title: 'home',
showLink: true,
savedPosition: false
}
}],
meta: {
icon: 'el-icon-s-home',
showLink: true,
savedPosition: false,
}
},
{
path: '/components',
name: 'components',
component: Layout,
redirect: '/components/split-pane',
children: [ children: [
{ {
path: '/components/video', path: "/welcome",
component: () => import(/* webpackChunkName: "components" */ '../views/components/video/index.vue'), name: "welcome",
component: () =>
import(/* webpackChunkName: "home" */ "../views/welcome.vue"),
meta: { meta: {
title: 'video', title: "home",
showLink: true,
savedPosition: false,
},
},
],
meta: {
icon: "el-icon-s-home",
showLink: true,
savedPosition: false,
},
},
{
path: "/components",
name: "components",
component: Layout,
redirect: "/components/split-pane",
children: [
{
path: "/components/video",
component: () =>
import(
/* webpackChunkName: "components" */ "../views/components/video/index.vue"
),
meta: {
title: "video",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/components/map', path: "/components/map",
component: () => import(/* webpackChunkName: "components" */ '../views/components/map/index.vue'), component: () =>
import(
/* webpackChunkName: "components" */ "../views/components/map/index.vue"
),
meta: { meta: {
title: 'map', title: "map",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/components/draggable', path: "/components/draggable",
component: () => import(/* webpackChunkName: "components" */ '../views/components/draggable/index.vue'), component: () =>
import(
/* webpackChunkName: "components" */ "../views/components/draggable/index.vue"
),
meta: { meta: {
title: 'draggable', title: "draggable",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/components/split-pane', path: "/components/split-pane",
component: () => import(/* webpackChunkName: "components" */ '../views/components/split-pane/index.vue'), component: () =>
import(
/* webpackChunkName: "components" */ "../views/components/split-pane/index.vue"
),
meta: { meta: {
title: 'split-pane', title: "split-pane",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/components/button', path: "/components/button",
component: () => import(/* webpackChunkName: "components" */ '../views/components/button/index.vue'), component: () =>
import(
/* webpackChunkName: "components" */ "../views/components/button/index.vue"
),
meta: { meta: {
title: 'button', title: "button",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/components/cropping', path: "/components/cropping",
component: () => import(/* webpackChunkName: "components" */ '../views/components/cropping/index.vue'), component: () =>
import(
/* webpackChunkName: "components" */ "../views/components/cropping/index.vue"
),
meta: { meta: {
title: 'cropping', title: "cropping",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/components/countTo', path: "/components/countTo",
component: () => import(/* webpackChunkName: "components" */ '../views/components/count-to/index.vue'), component: () =>
import(
/* webpackChunkName: "components" */ "../views/components/count-to/index.vue"
),
meta: { meta: {
title: 'countTo', title: "countTo",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/components/selector', path: "/components/selector",
component: () => import(/* webpackChunkName: "components" */ '../views/components/selector/index.vue'), component: () =>
import(
/* webpackChunkName: "components" */ "../views/components/selector/index.vue"
),
meta: { meta: {
title: 'selector', title: "selector",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/components/seamlessScroll', path: "/components/seamlessScroll",
component: () => import(/* webpackChunkName: "components" */ '../views/components/seamless-scroll/index.vue'), component: () =>
import(
/* webpackChunkName: "components" */ "../views/components/seamless-scroll/index.vue"
),
meta: { meta: {
title: 'seamless', title: "seamless",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
} },
// { // {
// path: '/components/flowChart', // path: '/components/flowChart',
// component: () => import(/* webpackChunkName: "components" */ '../views/components/flow-chart/index.vue'), // component: () => import(/* webpackChunkName: "components" */ '../views/components/flow-chart/index.vue'),
@ -124,146 +159,165 @@ const routes: Array<RouteRecordRaw> = [
// } // }
], ],
meta: { meta: {
icon: 'el-icon-menu', icon: "el-icon-menu",
title: 'components', title: "components",
showLink: true, showLink: true,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/flowChart', path: "/flowChart",
name: 'flowChart', name: "flowChart",
component: Layout, component: Layout,
redirect: '/flowChart/index', redirect: "/flowChart/index",
children: [ children: [
{ {
path: '/flowChart/index', path: "/flowChart/index",
component: () => import(/* webpackChunkName: "user" */ '../views/flow-chart/index.vue'), component: () =>
import(
/* webpackChunkName: "user" */ "../views/flow-chart/index.vue"
),
meta: { meta: {
title: 'flowChart', title: "flowChart",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
], ],
meta: { meta: {
icon: 'el-icon-set-up', icon: "el-icon-set-up",
title: 'flowChart', title: "flowChart",
showLink: true, showLink: true,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/editor', path: "/editor",
name: 'editor', name: "editor",
component: Layout, component: Layout,
redirect: '/editor/index', redirect: "/editor/index",
children: [ children: [
{ {
path: '/editor/index', path: "/editor/index",
component: () => import(/* webpackChunkName: "user" */ '../views/editor/index.vue'), component: () =>
import(/* webpackChunkName: "user" */ "../views/editor/index.vue"),
meta: { meta: {
// icon: 'el-icon-edit-outline', // icon: 'el-icon-edit-outline',
title: 'editor', title: "editor",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
], ],
meta: { meta: {
icon: 'el-icon-edit-outline', icon: "el-icon-edit-outline",
title: 'editor', title: "editor",
showLink: true, showLink: true,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/user', path: "/user",
name: 'user', name: "user",
component: Layout, component: Layout,
redirect: '/user/base', redirect: "/user/base",
children: [ children: [
{ {
path: '/user/base', path: "/user/base",
component: () => import(/* webpackChunkName: "user" */ '../views/user.vue'), component: () =>
import(/* webpackChunkName: "user" */ "../views/user.vue"),
meta: { meta: {
// icon: 'el-icon-user', // icon: 'el-icon-user',
title: 'baseinfo', title: "baseinfo",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
], ],
meta: { meta: {
icon: 'el-icon-user', icon: "el-icon-user",
title: 'usermanagement', title: "usermanagement",
showLink: true, showLink: true,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/error', path: "/error",
name: 'error', name: "error",
component: Layout, component: Layout,
redirect: '/error/401', redirect: "/error/401",
children: [ children: [
{ {
path: '/error/401', path: "/error/401",
component: () => import(/* webpackChunkName: "error" */ '../views/error/401.vue'), component: () =>
import(/* webpackChunkName: "error" */ "../views/error/401.vue"),
meta: { meta: {
title: '401', title: "401",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/error/404', path: "/error/404",
component: () => import(/* webpackChunkName: "error" */ '../views/error/404.vue'), component: () =>
import(/* webpackChunkName: "error" */ "../views/error/404.vue"),
meta: { meta: {
title: '404', title: "404",
showLink: false, showLink: false,
savedPosition: true savedPosition: true,
} },
}, },
], ],
meta: { meta: {
icon: 'el-icon-position', icon: "el-icon-position",
title: 'error', title: "error",
showLink: true, showLink: true,
savedPosition: true savedPosition: true,
} },
}, },
{ {
path: '/login', path: "/login",
name: 'login', name: "login",
component: () => import(/* webpackChunkName: "login" */ '../views/login.vue'), component: () =>
import(/* webpackChunkName: "login" */ "../views/login.vue"),
meta: { meta: {
title: '登陆', title: "登陆",
showLink: false showLink: false,
} },
}, },
{ {
path: '/register', path: "/register",
name: 'register', name: "register",
component: () => import(/* webpackChunkName: "register" */ '../views/register.vue'), component: () =>
import(/* webpackChunkName: "register" */ "../views/register.vue"),
meta: { meta: {
title: '注册', title: "注册",
showLink: false showLink: false,
} },
}, },
{ {
// 找不到路由重定向到404页面 // 找不到路由重定向到404页面
path: '/:pathMatch(.*)', path: "/:pathMatch(.*)",
component: Layout, component: Layout,
redirect: "/error/404", redirect: "/error/404",
meta: { meta: {
icon: 'el-icon-s-home', icon: "el-icon-s-home",
title: '首页', title: "首页",
showLink: false, showLink: false,
savedPosition: false, savedPosition: false,
} },
}, },
] {
path: "/redirect",
component: Layout,
children: [
{
path: "/redirect/:path(.*)",
component: () => import("../views/redirect.vue"),
},
],
},
];
const router = createRouter({ const router = createRouter({
history: createWebHashHistory(), history: createWebHashHistory(),
@ -271,30 +325,33 @@ const router = createRouter({
scrollBehavior(to, from, savedPosition) { scrollBehavior(to, from, savedPosition) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (savedPosition) { if (savedPosition) {
return savedPosition return savedPosition;
} else { } else {
if (from.meta.saveSrollTop) { if (from.meta.saveSrollTop) {
const top: number = document.documentElement.scrollTop || document.body.scrollTop const top: number =
resolve({ left: 0, top }) document.documentElement.scrollTop || document.body.scrollTop;
resolve({ left: 0, top });
} }
} }
}) });
} },
}) });
import NProgress from "../utils/progress" import NProgress from "../utils/progress";
const whiteList = ["/login", "/register"] const whiteList = ["/login", "/register"];
router.beforeEach((to, _from, next) => { router.beforeEach((to, _from, next) => {
NProgress.start() NProgress.start();
// @ts-ignore // @ts-ignore
document.title = to.meta.title // 动态title document.title = to.meta.title; // 动态title
whiteList.indexOf(to.path) !== -1 || storageSession.getItem("info") ? next() : next("/login") // 全部重定向到登录页 whiteList.indexOf(to.path) !== -1 || storageSession.getItem("info")
}) ? next()
: next("/login"); // 全部重定向到登录页
});
router.afterEach(() => { router.afterEach(() => {
NProgress.done() NProgress.done();
}) });
export default router export default router;

View File

@ -1,10 +1,9 @@
interface ProxyAlgorithm { interface ProxyAlgorithm {
increaseIndexes<T>(val: Array<T>): Array<T> increaseIndexes<T>(val: Array<T>): Array<T>;
} }
class algorithmProxy implements ProxyAlgorithm { class algorithmProxy implements ProxyAlgorithm {
constructor() {}
constructor() { }
// 数组每一项添加索引字段 // 数组每一项添加索引字段
public increaseIndexes<T>(val: Array<T>): Array<T> { public increaseIndexes<T>(val: Array<T>): Array<T> {
@ -13,12 +12,11 @@ class algorithmProxy implements ProxyAlgorithm {
return { return {
// @ts-ignore // @ts-ignore
...val[v], ...val[v],
key: v key: v,
} };
}) })
.filter(v => v.meta.showLink) .filter((v) => v.meta && v.meta.showLink);
} }
} }
export const algorithm = new algorithmProxy() export const algorithm = new algorithmProxy();

26
src/views/redirect.vue Normal file
View File

@ -0,0 +1,26 @@
<template>
<div></div>
</template>
<script lang="ts">
import { defineComponent, unref } from "vue";
import { useRouter } from "vue-router";
export default defineComponent({
name: "Redirect",
setup() {
const { currentRoute, replace } = useRouter();
const { params, query } = unref(currentRoute);
const { path } = params;
const _path = Array.isArray(path) ? path.join("/") : path;
replace({
path: "/" + _path,
query
});
return {};
}
});
</script>