mirror of
https://github.com/chansee97/nova-admin.git
synced 2025-04-05 19:41:59 +08:00
fix(router): 修正路由系统跳转错误
This commit is contained in:
parent
965d464f1f
commit
cd966ae137
@ -27,7 +27,7 @@ export function useAppRouter(isSetup = true) {
|
||||
|
||||
/* 跳转根页方法 */
|
||||
function toRoot() {
|
||||
routerPush({ name: 'appRoot' });
|
||||
routerPush({ path: '/appRoot' });
|
||||
}
|
||||
/* 跳转至登录页 */
|
||||
function toLogin(redirectUrl?: string) {
|
||||
|
@ -45,7 +45,7 @@ export async function createDynamicRoutes(routes: AppRoute.Route[]) {
|
||||
});
|
||||
|
||||
const appRootRoute: RouteRecordRaw = {
|
||||
path: '/',
|
||||
path: '/appRoot',
|
||||
name: 'appRoot',
|
||||
redirect: '/dashboard/workbench',
|
||||
component: BasicLayout,
|
||||
|
@ -1,8 +1,7 @@
|
||||
import type { Router } from 'vue-router';
|
||||
|
||||
import { createPermissionGuard } from './permission';
|
||||
|
||||
import { useAppInfo } from '@/hooks';
|
||||
|
||||
const { title } = useAppInfo();
|
||||
|
||||
export function setupRouterGuard(router: Router) {
|
||||
|
@ -7,39 +7,40 @@ export async function createPermissionGuard(
|
||||
from: RouteLocationNormalized,
|
||||
next: NavigationGuardNext,
|
||||
) {
|
||||
const isLogin = Boolean(getToken());
|
||||
|
||||
const routeStore = useRouteStore();
|
||||
const tabStore = useTabStore();
|
||||
|
||||
// 判断有无TOKEN,登录鉴权
|
||||
const isLogin = Boolean(getToken());
|
||||
if (!isLogin) {
|
||||
if (to.name === 'login') {
|
||||
next();
|
||||
} else {
|
||||
// login除了404,其余在登录后重定向到之前的网页
|
||||
const redirect = to.name === 'not-found' ? undefined : to.fullPath;
|
||||
next({ path: '/login', query: { redirect } });
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 判断路由有无进行初始化
|
||||
if (!routeStore.isInitAuthRoute) {
|
||||
// 没有初始化路由 => 登录鉴权
|
||||
// 登录鉴权
|
||||
if (!isLogin) {
|
||||
if (to.name === 'login') {
|
||||
next();
|
||||
} else {
|
||||
// login除了404,其余在登录后重定向到之前的网页
|
||||
const redirect = to.name === 'not-found' ? undefined : to.fullPath;
|
||||
next({ path: '/login', query: { redirect } });
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// 有登录但是没有路由,初始化路由、侧边菜单等
|
||||
await routeStore.initAuthRoute();
|
||||
// 动态路由加载完回到根路由
|
||||
if (to.name === 'not-found') {
|
||||
// 动态路由没有加载导致被not-found-page路由捕获,等待权限路由加载好了,回到之前的路由
|
||||
// 若路由是从根路由重定向过来的,重新回到根路由
|
||||
// 等待权限路由加载好了,回到之前的路由,否则404
|
||||
const path = to.redirectedFrom?.fullPath;
|
||||
next({ path, replace: true, query: to.query, hash: to.hash });
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 权限路由已经加载,仍然未找到,重定向到not-found
|
||||
// if (to.name === 'not-found-page') {
|
||||
// next({ name: 'not-found-page', replace: true });
|
||||
// }
|
||||
|
||||
// 判断当前页是否在login,则定位去首页
|
||||
if (to.name === 'login') {
|
||||
next({ path: '/appRoot' })
|
||||
}
|
||||
|
||||
// 设置菜单高亮
|
||||
if (to.meta.activeMenu) {
|
||||
routeStore.setActiveMenu(to.meta.activeMenu);
|
||||
|
@ -6,7 +6,7 @@ export const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
path: '/',
|
||||
name: 'root',
|
||||
redirect: 'appRoot',
|
||||
redirect: '/appRoot',
|
||||
component: BasicLayout,
|
||||
children: [
|
||||
{
|
||||
|
@ -105,12 +105,12 @@ export const useRouteStore = defineStore('route-store', {
|
||||
/* 初始化动态路由 */
|
||||
async initDynamicRoute() {
|
||||
// 根据用户id来获取用户的路由
|
||||
const { userId } = getUserInfo();
|
||||
const { userId } = getUserInfo()
|
||||
const { data: routes } = await fetchUserRoutes({ userId });
|
||||
// 根据用户返回的路由表来生成真实路由
|
||||
const appRoutes = await createDynamicRoutes(routes);
|
||||
// 生成侧边菜单
|
||||
await this.createMenus(routes);
|
||||
this.createMenus(routes);
|
||||
// 插入路由表
|
||||
router.addRoute(appRoutes);
|
||||
},
|
||||
@ -119,7 +119,7 @@ export const useRouteStore = defineStore('route-store', {
|
||||
// 根据静态路由表来生成真实路由
|
||||
const appRoutes = await createDynamicRoutes(staticRoutes);
|
||||
// 生成侧边菜单
|
||||
await this.createMenus(staticRoutes);
|
||||
this.createMenus(staticRoutes);
|
||||
// 插入路由表
|
||||
router.addRoute(appRoutes);
|
||||
},
|
||||
|
@ -12,6 +12,7 @@
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"lib": ["ESNext", "DOM"],
|
||||
"skipLibCheck": true,
|
||||
"paths": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user