Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: first level menu external link demo #102

Merged
merged 1 commit into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions arco-design-pro-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"build": "vue-tsc --noEmit && vite build --config ./config/vite.config.prod.ts",
"report": "cross-env REPORT=true npm run build",
"preview": "npm run build && vite preview --host",
"type:check": "vue-tsc --noEmit --skipLibCheck",
"lint-staged": "npx lint-staged",
"prepare": "husky install"
},
Expand Down
20 changes: 4 additions & 16 deletions arco-design-pro-vite/src/components/menu/useMenuTree.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import { computed } from 'vue';
import { useRouter, RouteRecordRaw, RouteRecordNormalized } from 'vue-router';
import { RouteRecordRaw, RouteRecordNormalized } from 'vue-router';
import usePermission from '@/hooks/permission';
import { useAppStore } from '@/store';
import appClientMenus from '@/router/appMenus';

export default function useMenuTree() {
const router = useRouter();
const permission = usePermission();
const appStore = useAppStore();
const appRoute = computed(() => {
if (appStore.menuFromServer) {
return appStore.appServerMenuConfig;
return appStore.appAsyncMenus;
}
return router
.getRoutes()
.filter((el) => el.meta.requiresAuth && el.meta.order !== undefined)
.map((el) => {
const { name, path, meta, redirect, children } = el;
return {
name,
path,
meta,
redirect,
children,
};
});
return appClientMenus;
});
const menuTree = computed(() => {
const copyRouter = JSON.parse(JSON.stringify(appRoute.value));
Expand Down
2 changes: 2 additions & 0 deletions arco-design-pro-vite/src/locale/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export default {
'menu.profile': 'Profile',
'menu.visualization': 'Data Visualization',
'menu.user': 'User Center',
'menu.arcoWebsite': 'Arco Design',
'menu.faq': 'FAQ',
'navbar.docs': 'Docs',
'navbar.action.locale': 'Switch to English',
...localeSettings,
Expand Down
2 changes: 2 additions & 0 deletions arco-design-pro-vite/src/locale/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export default {
'menu.profile': '详情页',
'menu.visualization': '数据可视化',
'menu.user': '个人中心',
'menu.arcoWebsite': 'Arco Design',
'menu.faq': '常见问题',
'navbar.docs': '文档中心',
'navbar.action.locale': '切换为中文',
...localeSettings,
Expand Down
10 changes: 9 additions & 1 deletion arco-design-pro-vite/src/mock/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ setupMock({
Mock.mock(new RegExp('/api/user/menu'), () => {
const menuList = [
{
path: 'dashboard',
path: '/dashboard',
name: 'dashboard',
meta: {
locale: 'menu.server.dashboard',
Expand All @@ -96,6 +96,14 @@ setupMock({
roles: ['admin'],
},
},
{
path: 'https://arco.design',
name: 'arcoWebsite',
meta: {
locale: 'menu.arcoWebsite',
requiresAuth: true,
},
},
],
},
];
Expand Down
16 changes: 16 additions & 0 deletions arco-design-pro-vite/src/router/appMenus/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { appRoutes, appExternalRoutes } from '../routes';

const mixinRoutes = [...appRoutes, ...appExternalRoutes];

const appClientMenus = mixinRoutes.map((el) => {
const { name, path, meta, redirect, children } = el;
return {
name,
path,
meta,
redirect,
children,
};
});

export default appClientMenus;
2 changes: 1 addition & 1 deletion arco-design-pro-vite/src/router/guard/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import NProgress from 'nprogress'; // progress bar
import usePermission from '@/hooks/permission';
import { useUserStore } from '@/store';
import { isLogin } from '@/utils/auth';
import appRoutes from '../routes';
import { appRoutes } from '../routes';

export default function setupPermissionGuard(router: Router) {
router.beforeEach(async (to, from, next) => {
Expand Down
2 changes: 1 addition & 1 deletion arco-design-pro-vite/src/router/guard/serverPermission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function setupServerPermissionGuard(router: Router) {
// Refine the permission logic from the server's menu configuration as needed
const appStore = useAppStore();
if (appStore.menuFromServer) {
const serverMenuConfig = [...appStore.appServerMenuConfig, ...whiteList];
const serverMenuConfig = [...appStore.appAsyncMenus, ...whiteList];
let exist = false;
while (serverMenuConfig.length) {
const element = serverMenuConfig.shift();
Expand Down
2 changes: 1 addition & 1 deletion arco-design-pro-vite/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createRouter, createWebHistory } from 'vue-router';
import NProgress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css';

import appRoutes from './routes';
import { appRoutes } from './routes';
import createRouteGuard from './guard';

NProgress.configure({ showSpinner: false }); // NProgress Configuration
Expand Down
10 changes: 10 additions & 0 deletions arco-design-pro-vite/src/router/routes/externalModules/arco.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
path: 'https://arco.design',
name: 'arcoWebsite',
meta: {
locale: 'menu.arcoWebsite',
icon: 'icon-link',
requiresAuth: true,
order: 8,
},
};
10 changes: 10 additions & 0 deletions arco-design-pro-vite/src/router/routes/externalModules/faq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
path: 'https://arco.design/vue/docs/pro/faq',
name: 'faq',
meta: {
locale: 'menu.faq',
icon: 'icon-question-circle',
requiresAuth: true,
order: 9,
},
};
34 changes: 21 additions & 13 deletions arco-design-pro-vite/src/router/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import type { RouteRecordRaw } from 'vue-router';
import type { RouteRecordNormalized } from 'vue-router';

const modules = import.meta.globEager('./modules/*.ts');
const appRoutes: RouteRecordRaw[] = [];

Object.keys(modules).forEach((key) => {
const defaultModule = modules[key].default;
if (!defaultModule) return;
const moduleList = Array.isArray(defaultModule)
? [...defaultModule]
: [defaultModule];
appRoutes.push(...moduleList);
});

export default appRoutes;
const externalModules = import.meta.globEager('./externalModules/*.ts');

function formatModules(_modules: any, result: RouteRecordNormalized[]) {
Object.keys(_modules).forEach((key) => {
const defaultModule = _modules[key].default;
if (!defaultModule) return;
const moduleList = Array.isArray(defaultModule)
? [...defaultModule]
: [defaultModule];
result.push(...moduleList);
});
return result;
}

export const appRoutes: RouteRecordNormalized[] = formatModules(modules, []);

export const appExternalRoutes: RouteRecordNormalized[] = formatModules(
externalModules,
[]
);
2 changes: 1 addition & 1 deletion arco-design-pro-vite/src/store/modules/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const useAppStore = defineStore('app', {
appDevice(state: AppState) {
return state.device;
},
appServerMenuConfig(state: AppState): RouteRecordNormalized[] {
appAsyncMenus(state: AppState): RouteRecordNormalized[] {
return state.serverMenu as unknown as RouteRecordNormalized[];
},
},
Expand Down