Skip to content

Commit

Permalink
refactor(projects): add createRoutes replace export routes directly
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Nov 10, 2023
1 parent 76400a8 commit 16bca45
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
createMemoryHistory,
type RouterHistory
} from 'vue-router';
import { constantVueRoutes } from './routes';
import { createRoutes } from './routes';
import { createRouterGuard } from './guard';

const { VITE_ROUTER_HISTORY_MODE = 'history', VITE_BASE_URL } = import.meta.env;
Expand All @@ -17,6 +17,8 @@ const historyCreatorMap: Record<Env.RouterHistoryMode, (base?: string) => Router
memory: createMemoryHistory
};

const { constantVueRoutes } = createRoutes();

export const router = createRouter({
history: historyCreatorMap[VITE_ROUTER_HISTORY_MODE](VITE_BASE_URL),
routes: constantVueRoutes
Expand Down
10 changes: 7 additions & 3 deletions src/router/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const customRoutes: CustomRoute[] = [
}
];

/**
* create routes
*/
export function createRoutes() {
const constantRoutes: ElegantRoute[] = [];

Expand All @@ -42,14 +45,15 @@ export function createRoutes() {
const constantVueRoutes = transformElegantRoutesToVueRoutes(constantRoutes, layouts, views);

return {
constantRoutes,
constantVueRoutes,
authRoutes
};
}

export const { constantRoutes, constantVueRoutes, authRoutes } = createRoutes();

/**
* get auth vue routes
* @param routes elegant routes
*/
export function getAuthVueRoutes(routes: ElegantConstRoute[]) {
return transformElegantRoutesToVueRoutes(routes, layouts, views);
}
8 changes: 7 additions & 1 deletion src/store/modules/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useBoolean } from '@sa/hooks';
import type { ElegantConstRoute, CustomRoute, RouteKey, LastLevelRouteKey, RouteMap } from '@elegant-router/types';
import { SetupStoreId } from '@/enum';
import { router } from '@/router';
import { authRoutes, constantVueRoutes, getAuthVueRoutes, ROOT_ROUTE } from '@/router/routes';
import { createRoutes, getAuthVueRoutes, ROOT_ROUTE } from '@/router/routes';
import { getRoutePath, getRouteName } from '@/router/elegant/transform';
import { fetchGetUserRoutes, fetchIsRouteExist } from '@/service/api';
import {
Expand Down Expand Up @@ -77,6 +77,8 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
* @param routes vue routes
*/
function getCacheRoutes(routes: RouteRecordRaw[]) {
const { constantVueRoutes } = createRoutes();

cacheRoutes.value = getCacheRouteNames([...constantVueRoutes, ...routes]);
}

Expand Down Expand Up @@ -165,6 +167,8 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
* init static auth route
*/
async function initStaticAuthRoute() {
const { authRoutes } = createRoutes();

const filteredAuthRoutes = filterAuthRoutesByRoles(authRoutes, authStore.userInfo.roles);

handleAuthRoutes(filteredAuthRoutes);
Expand Down Expand Up @@ -252,6 +256,8 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
}

if (authRouteMode.value === 'static') {
const { authRoutes } = createRoutes();

return isRouteExistByRouteName(routeName, authRoutes);
}

Expand Down

0 comments on commit 16bca45

Please sign in to comment.