Commit f9a2760f authored by 李鹏 's avatar 李鹏

mofigy

parent 036e934a
......@@ -3,44 +3,8 @@
import { createRouter, createWebHashHistory } from "vue-router";
import store from "../store";
//写入初始必须有的路由
//或者隐藏式路由,不会表现在菜单上,但是需要存在的路由
<<<<<<< Updated upstream
const routes = [{
path: "/login",
name: "login",
show: true,
component: () =>
import ("../page/login/index.vue"),
},
{
path: "/register",
name: "register",
show: true,
component: () =>
import ("../page/register/index.vue"),
},
{
path: "/password",
name: "password",
show: true,
component: () =>
import ("../page/password/index.vue"),
},
{
path: "/",
name: "index",
component: () =>
import ("../page/welcom.vue"),
},
{
path: "/404",
show: true,
component: () =>
import ("../page/404.vue"),
},
=======
const routes = [
{
path: "/login",
......@@ -64,7 +28,6 @@ const routes = [
show: true,
component: () => import("../page/404.vue"),
},
>>>>>>> Stashed changes
];
//重新创建router
......@@ -79,37 +42,26 @@ const router = newRouterFunc();
function inWhiteList(toPath) {
//配置白名单
const whiteList = ['/login']
const whiteList = ["/login"];
const path = whiteList.find((value) => {
// 使用正则匹配
const reg = new RegExp('^' + value)
return reg.test(toPath)
})
return !!path
const reg = new RegExp("^" + value);
return reg.test(toPath);
});
return !!path;
}
router.beforeEach((to, from, next) => {
<<<<<<< Updated upstream
=======
const userInfo = store.state.userInfo;
//已登录不可跳转登陆页 || to.path == "/" 去除首页跳转判断
if (userInfo && to.path == "/login") {
return next({
path: "/",
});
}
>>>>>>> Stashed changes
const userInfo = store.state.userInfo
//已登录不可跳转登陆页
if (userInfo && to.path == '/login') {
next({ path: from.path })
if (userInfo && to.path == "/login") {
next({ path: from.path });
}
// 检查to.path是否存在于免登陆白名单
if (inWhiteList(to.path)) {
next()
next();
} else {
// 判断是否已经登录,未登录则重定向到首页或其他页面(通过query传参记录原来的路径)
// 根据配置判断是否跳转第三方登录,跳转第三方登录则不跳login
......@@ -119,18 +71,16 @@ router.beforeEach((to, from, next) => {
// path: '/login',
// })
// } else {
// }
}
//判读是否匹配,否则跳转404
if (to.matched.length !== 0) {
next()
next();
} else {
return next({ path: '/404' })
return next({ path: "/404" });
}
})
});
// 在路由完成初始导航时调用,如果有异步操作放置到这里
// 请求相应的角色和菜单
......@@ -138,38 +88,33 @@ router.beforeEach((to, from, next) => {
// generateRoutes()
// })
function addRoute(router, routers) {
routers.forEach(e => {
router.addRoute(e)
routers.forEach((e) => {
router.addRoute(e);
if (e.children && e.children.length) {
addRoute(router, e.children)
addRoute(router, e.children);
}
});
}
export function generateRoutes() {
const _asyncRoutes = store.state.route
const _asyncRoutes = store.state.route;
if (_asyncRoutes) {
//动态添加路由
addRoute(router, _asyncRoutes)
addRoute(router, _asyncRoutes);
}
router.addRoute({
path: '/:pathMatch(.*)',
redirect: '/404'
})
path: "/:pathMatch(.*)",
redirect: "/404",
});
}
//新创建一个router替代之前的router,并把matcher方法替换成新的router的matcher
export function resetRouter() {
const newRouter = newRouterFunc()
router.matcher = newRouter.matcher
const newRouter = newRouterFunc();
router.matcher = newRouter.matcher;
}
export default router
\ No newline at end of file
export default router;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment