Route Structure
One-level route (single-level route)
Folder structure
views
├── about
│ └── index.vue
Generated routes
{
name: 'about',
path: '/about',
component: 'layout.base$view.about',
meta: {
title: 'about'
}
},
it is a single level route, to add layout, the component props combines the layout and view component, split by the dollar sign "$"
Transformed Vue routes
{
path: '/about',
component: BaseLayout,
children: [
{
name: 'about',
path: '',
component: () => import('@/views/about/index.vue'),
meta: {
title: 'about'
}
}
]
},
Secondary route
Folder structure
views
├── list
│ ├── home
│ │ └── index.vue
│ ├── detail
│ │ └── index.vue
Please don't have the following index.vue on the same level as the folder, this is not part of the agreed upon rules
Error example
views
├── list
│ ├── index.vue
│ ├── detail
│ │ └── index.vue
Generated routes
{
name: 'list',
path: '/list',
component: 'layout.base',
meta: {
title: 'list'
},
children: [
{
name: 'list_home',
path: '/list/home',
component: 'view.list_home',
meta: {
title: 'list_home'
}
},
{
name: 'list_detail',
path: '/list/detail',
component: 'view.list_detail',
meta: {
title: 'list_detail'
}
},
]
}
There are two layers of route data for secondary routes, the first layer of route is the layout component and the second layer of route is the page component
Transformed Vue routes
{
name: 'list',
path: '/list',
component: BaseLayout,
redirect: {
name: 'list_home'
},
meta: {
title: 'list'
},
children: [
{
name: 'list_home',
path: '/list/home',
component: () => import('@/views/list/home/index.vue'),
meta: {
title: 'list_home'
}
},
{
name: 'list_detail',
path: '/list/detail',
component: () => import('@/views/list/detail/index.vue'),
meta: {
title: 'list_detail'
}
}
]
},
the first layer of route data contains the redirection configuration, which by default redirects to the first sub-route
Multi-level route (level 3 route and above)
Folder structure
- The folder hierarchy is deep
views
├── multi-menu
│ ├── first
│ │ ├── child
│ │ │ └── index.vue
│ ├── second
│ │ ├── child
│ │ │ ├── home
│ │ │ │ └── index.vue
- Two-tier folder hierarchy (recommended)
views
├── multi-menu
│ ├── first_child
│ │ └── index.vue
│ ├── second_child_home
│ │ └── index.vue
The route hierarchy is split by the underscore symbol "_", which prevents the folder hierarchy from being too deep.
Generated routes
{
name: 'multi-menu',
path: '/multi-menu',
component: 'layout.base',
meta: {
title: 'multi-menu'
},
children: [
{
name: 'multi-menu_first',
path: '/multi-menu/first',
meta: {
title: 'multi-menu_first'
},
children: [
{
name: 'multi-menu_first_child',
path: '/multi-menu/first/child',
component: 'view.multi-menu_first_child',
meta: {
title: 'multi-menu_first_child'
}
}
]
},
{
name: 'multi-menu_second',
path: '/multi-menu/second',
meta: {
title: 'multi-menu_second'
},
children: [
{
name: 'multi-menu_second_child',
path: '/multi-menu/second/child',
meta: {
title: 'multi-menu_second_child'
},
children: [
{
name: 'multi-menu_second_child_home',
path: '/multi-menu/second/child/home',
component: 'view.multi-menu_second_child_home',
meta: {
title: 'multi-menu_second_child_home'
}
}
]
}
]
}
]
}
if the route level is greater than 2, the generated route data is a recursive structure
Transformed Vue routes
{
name: 'multi-menu',
path: '/multi-menu',
component: BaseLayout,
redirect: {
name: 'multi-menu_first'
},
meta: {
title: 'multi-menu'
},
children: [
{
name: 'multi-menu_first',
path: '/multi-menu/first',
redirect: {
name: 'multi-menu_first_child'
},
meta: {
title: 'multi-menu_first'
}
},
{
name: 'multi-menu_first_child',
path: '/multi-menu/first/child',
component: () => import('@/views/multi-menu/first_child/index.vue'),
meta: {
title: 'multi-menu_first_child'
}
},
{
name: 'multi-menu_second',
path: '/multi-menu/second',
redirect: {
name: 'multi-menu_second_child'
},
meta: {
title: 'multi-menu_second'
},
},
{
name: 'multi-menu_second_child',
path: '/multi-menu/second/child',
redirect: {
name: 'multi-menu_second_child_home'
},
meta: {
title: 'multi-menu_second_child'
},
},
{
name: 'multi-menu_second_child_home',
path: '/multi-menu/second/child/home',
component: () => import('@/views/multi-menu/second_child_home/index.vue'),
meta: {
title: 'multi-menu_second_child_home'
}
}
]
},
the transformed Vue routes only has two levels, the first level is the layout component, and the second level is the redirect routes or the page routes
Ignore folder aggregation routes
Folder names that begin with an underscore "_" will be ignored
Folder structure
views
├── _error
│ ├── 403
│ │ └── index.vue
│ ├── 404
│ │ └── index.vue
│ ├── 500
│ │ └── index.vue
Generated routes
{
name: '403',
path: '/403',
component: 'layout.base$view.403',
meta: {
title: '403'
}
},
{
name: '404',
path: '/404',
component: 'layout.base$view.404',
meta: {
title: '404'
}
},
{
name: '500',
path: '/500',
component: 'layout.base$view.500',
meta: {
title: '500'
}
},
Parameter Route
Folder structure
views
├── user
│ └── [id].vue
Generated routes
{
name: 'user',
path: '/user/:id',
component: 'layout.base$view.user',
props: true,
meta: {
title: 'user'
}
}
Advanced parameter route
import type { RouteKey } from '@elegant-router/types';
ElegantVueRouter({
routePathTransformer(routeName, routePath) {
const routeKey = routeName as RouteKey;
if (routeKey === 'user') {
return '/user/:id(\\d+)';
}
return routePath;
}
});
Custom Route
the custom route is only used to generate the route declaration, and the route data is not generated, you should create the route data manually.
Config custom routes
ElegantVueRouter({
customRoutes: {
map: {
root: '/',
notFound: '/:pathMatch(.*)*'
},
names: ['two-level_route']
}
});
Generated CustomRouteKey
type RouteMap = {
root: '/';
notFound: '/:pathMatch(.*)*';
'two-level': '/two-level';
'two-level_route': '/two-level/route';
};
type CustomRouteKey = 'root' | 'notFound' | 'two-level' | 'two-level_route';
Custom routes's component
it can use existing page components as the route component
import type { CustomRoute } from '@elegant-router/types';
const customRoutes: CustomRoute[] = [
{
name: 'root',
path: '/',
redirect: {
name: '403'
}
},
{
name: 'not-found',
path: '/:pathMatch(.*)*',
component: 'layout.base$view.404'
},
{
name: 'two-level',
path: '/two-level',
component: 'layout.base',
children: [
{
name: 'two-level_route',
path: '/two-level/route',
component: 'view.about'
}
]
}
];