Skip to content

Commit 0e31a71

Browse files
committed
fix: rotue RelativePath, attach meta.params .params
1 parent ad17111 commit 0e31a71

File tree

6 files changed

+75
-19
lines changed

6 files changed

+75
-19
lines changed

examples/layouts/BasicLayout.vue

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
>
1111
<template #menuHeaderRender>
1212
<a>
13-
<img src="/public/antdv-pro-logo.svg" />
13+
<img src="/antdv-pro-logo.svg" />
1414
<h1>Pro Layout</h1>
1515
</a>
1616
</template>
@@ -31,12 +31,8 @@
3131
</template>
3232
<!-- custom breadcrumb itemRender -->
3333
<template #breadcrumbRender="{ route, params, routes }">
34-
<span v-if="routes.indexOf(route) === routes.length - 1">
35-
{{ route.breadcrumbName }}
36-
</span>
37-
<router-link v-else :to="{ path: route.path, params }">
38-
{{ route.breadcrumbName }}
39-
</router-link>
34+
<span v-if="routes.indexOf(route) === routes.length - 1">{{ route.breadcrumbName }}</span>
35+
<router-link v-else :to="{ path: route.path, params }">{{ route.breadcrumbName }}</router-link>
4036
</template>
4137
<template #menuExtraRender="{ collapsed }">
4238
<a-input-search v-if="!collapsed" @search="handleSearch" />
@@ -136,6 +132,8 @@ export default defineComponent({
136132
const router = useRouter();
137133
const { menuData } = getMenuData(clearMenuItem(router.getRoutes()));
138134
135+
console.log('menuData Paths:', menuData);
136+
139137
const baseState = reactive<Omit<RouteContextProps, 'menuData'>>({
140138
selectedKeys: [],
141139
openKeys: [],

examples/layouts/CustomLayout.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
>
1111
<template #menuHeaderRender>
1212
<a>
13-
<img src="/public/antdv-pro-logo.svg" />
13+
<img src="/antdv-pro-logo.svg" />
1414
<h1>Pro Layout</h1>
1515
</a>
1616
</template>
@@ -47,9 +47,7 @@
4747
>
4848
<router-link :to="{ path: item.path }">
4949
<div class="a-menu-item-title">
50-
<a-badge count="5" dot>
51-
{{ item.meta.title }}
52-
</a-badge>
50+
<a-badge count="5" dot>{{ item.meta.title }}</a-badge>
5351
</div>
5452
</router-link>
5553
</a-menu-item>

examples/router.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ const routes: RouteRecordRaw[] = [
1919
component: RouteView,
2020
children: [
2121
{
22-
path: 'workspace',
22+
path: '/dashboard/workspace',
2323
name: 'workspace',
2424
meta: { title: 'Workspace', icon: 'icon-antdesign' },
2525
component: () => import('./views/page1.vue'),
2626
},
2727
{
28-
path: 'monitor',
28+
path: '/dashboard/monitor',
2929
name: 'monitor',
3030
meta: { title: 'Monitor', icon: 'icon-icon-test' },
3131
component: () => import('./views/page2.vue'),
@@ -45,6 +45,18 @@ const routes: RouteRecordRaw[] = [
4545
meta: { title: 'Basic Form' },
4646
component: () => import('./views/page1.vue'),
4747
},
48+
{
49+
path: 'description-form/:id(\\d+)?',
50+
name: 'description-form',
51+
meta: {
52+
title: 'Description',
53+
// attach `params` to `$route.params`
54+
params: {
55+
id: 2,
56+
},
57+
},
58+
component: () => import('./views/dynamic-page.vue'),
59+
},
4860
],
4961
},
5062
{

examples/views/dynamic-page.vue

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<template>
2+
<page-container :title="`${$route.meta.title} ${$route.params.id}`" sub-title="is a sub-title.">
3+
<template #content>
4+
<div>
5+
container.content
6+
<h1>{{ text }}</h1>
7+
</div>
8+
</template>
9+
<span>page-content</span>
10+
<a-button @click="handleClick">Button</a-button>
11+
<p v-for="item in new Array(20)" :key="item">text block...</p>
12+
</page-container>
13+
</template>
14+
15+
<script lang="ts">
16+
import { defineComponent, ref } from 'vue';
17+
import { Button, message } from 'ant-design-vue';
18+
19+
export default defineComponent({
20+
components: {
21+
[Button.name]: Button,
22+
},
23+
setup() {
24+
const text = ref<string>('1');
25+
const handleClick = () => {
26+
message.info('clicked');
27+
text.value = `${Math.random()}`;
28+
};
29+
return {
30+
handleClick,
31+
32+
text,
33+
};
34+
},
35+
});
36+
</script>

src/SiderMenu/BaseMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class MenuUtil {
216216
const target = (meta.target || null) as string | null;
217217
const hasUrl = isUrl(item.path);
218218
const CustomTag: any = (target && 'a') || this.RouterLink;
219-
const props = { to: { name: item.name } };
219+
const props = { to: { name: item.name, ...item.meta } };
220220
const attrs = hasUrl || target ? { ...item.meta, href: item.path, target } : {};
221221

222222
const { prefixCls, locale } = this.props;

src/utils/getMenuData.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ export const formatRelativePath = (
1414
): RouteRecordRaw[] => {
1515
// 计算路由绝对路径
1616
return routes.map(route => {
17-
if (parent) {
18-
route.path = `${parent.path || ''}/${route.path}`;
19-
} else {
20-
route.path = `/${route.path}`;
17+
// Note that nested paths that start with / will be treated as a root path.
18+
// This allows you to leverage the component nesting without having to use a nested URL.
19+
// @ref https://router.vuejs.org/guide/essentials/nested-routes.html#nested-routes
20+
const hasRelativePath = route.path.startsWith('/');
21+
if (!hasRelativePath) {
22+
if (parent) {
23+
route.path = `${parent.path || ''}/${route.path}`;
24+
} else {
25+
route.path = `/${route.path}`;
26+
}
2127
}
28+
29+
// reformat path
2230
route.path = route.path.replace('//', '/');
2331
// format children routes
2432
if (route.children && route.children.length > 0) {
@@ -42,7 +50,11 @@ export const getMenuData = (routes: RouteRecordRaw[], child?: RouteRecordRaw): M
4250
);
4351
const breadcrumb: Record<string, any> = {};
4452
return {
45-
menuData: formatRelativePath(childrenRoute?.children || ([] as RouteRecordRaw[]), breadcrumb),
53+
menuData: formatRelativePath(
54+
childrenRoute?.children || ([] as RouteRecordRaw[]),
55+
breadcrumb,
56+
undefined,
57+
),
4658
breadcrumb,
4759
};
4860
};

0 commit comments

Comments
 (0)