# 路由

# 约定式路由

约定views下的目录结构,自动生成路由。

规则如下:

  • 目录结构的层级即路由层级(路由中的 children)
  • 如果模块或枝页面下面没有index.vue,会用LWrapper组件填充
  • 如果是views目录,会自动忽略该层
  • 以下目录名为保留字,不会当作路由处理:layout, components, directives, filters, mixins, utils, styles, service, module

下面举个例子:

├─ moduleA/                 # 模块 A
│   ├─ module/              # 模块配置
│   ├─ components/          # 模块级组件
│   │   ├─ s-detail.vue     # 详情组件
│   │   │   │   └─ ...
│   ├─ utils/               # 模块级工具库
│   ├─ views/
│   │   ├─ list.vue         # 模块 A 的列表页面
│   │   ├─ detail/          # 模块 A 的详情页面
│   │   │   ├─ index.vue    # 模块 A 的详情页面内容
│   │   │   ├─ info.vue     # 模块 A 的详情信息页面
│   │   │   ├─ monitor.vue  # 模块 A 的详情图表页面
│   │   └─ ...
1
2
3
4
5
6
7
8
9
10
11
12
13

将会生成

export default {
    path: 'moduleA',
    component: LWrapper,
    children: [
        { path: '', redirect: 'list' },
        { path: 'list', component: () => import(/* webpackChunkName: 'moduleA' */ './views/list.vue') },
        { path: 'detail', component: () => import(/* webpackChunkName: 'moduleA' */ './views/index.vue'), children: [
            { path: '', redirect: 'info' },
            { path: 'info', component: () => import(/* webpackChunkName: 'moduleA' */ './views/detail/info.vue') },
            { path: 'monitor', component: () => import(/* webpackChunkName: 'moduleA' */ './views/detail/monitor.vue') },
        ] },
    ],
};
1
2
3
4
5
6
7
8
9
10
11
12
13
上次更新: 2023年06月26日