Skip to content
On this page

路由配置

路由是整套系统中最重要的存在,和系统左侧导航、页面标题甚至页面缓存都存在一定的关系。路由分为 系统路由自定义路由 两部分。

系统路由

当某些页面与业务脱节,而且基本上不会有变动页面,比如403404/login/register等的页面可作为系统路由固定在路由表。 系统路由 定义在 @/router/index.ts,并且此文件包含全局路由守卫代码。

自定义路由

自定义路由 定义在 @/config/routes.ts,在路由配置中可以配置路由的标题,图标,是否在菜单中显示等。

路由定义

ts
export type CustomRouteMeta = {
    affix?: boolean;            // 标签是否置顶
    keepAlive?: boolean;        // 是否缓存页面
    hideInMenu?: boolean;       // 是否在菜单中隐藏
    hideInBreadcrumb?: boolean; // 是否在面包屑中隐藏
    hideInTag?: boolean;        // 是否在标签中隐藏
    auth?: boolean;             // 是否开启权限控制
    title: string;              // 页面标题
    icon?: string;              // 页面图标
};
export type CustomRouteRaw = {
    meta?: CustomRouteMeta;
    name?: string;               // 路由名称
    path?: string;               // 页面路由
    link?: string;               // 外链地址
    redirect?: string;           // 默认重定向地址
    children?: CustomRouteRaw[]; // 子页面配置
    component?: string;          // 组件
};

api

CustomRouteRaw

属性名说明类型默认值说明
meta路由元数据CustomRouteMeta-见下表
name路由名称string-没有设置路由名称会自动生成一个10位的随机字符串来代替
path页面路由string-path字段和link字段必须存在一个
link外链地址string-path字段和link字段必须存在一个
redirect默认重定向地址string-必须设置path字段才能生效
component组件路径string-需要以@/views开头,组件结尾如果为index.vue时可省略
children子页面配置CustomRouteRaw[][]

CustomRouteMeta

属性名说明类型默认值说明
affix标签是否置顶booleanfalse置顶以后的标签不可关闭
keepAlive是否缓存页面booleantrue默认开启页面缓存,缓存以后的组件新增activedeactive两个生命周期
hideInMenu是否在菜单中隐藏booleanfalse
hideInBreadcrumb是否在面包屑中隐藏booleanfalse
hideInTag是否在标签中隐藏booleanfalse
auth是否开启权限控制booleantrue关闭以后此路由及子路由不再进行权限验证,可以任意访问
title页面标题string-路由标题,此项必须设置
icon页面图标string-菜单图标,可设置element plus中的图标,例如:el-icon-linkElIconLinkLink

路由示例

ts
import type { CustomRouteRaw } from "@/router/type";

export default [
    {
        path: "/",
        redirect: "/dashboard",
        meta: {
            hideInMenu: true,
        },
    },
    {
        path: "/dashboard",
        meta: {
            title: "首页",
            affix: true,
        },
        component: "@/views/dashboard",
    },
    {
        path: "/channel",
        meta: {
            title: "渠道管理",
            icon: "Suitcase",
        },
        children: [
            {
                path: "manager",
                meta: {
                    title: "渠道经理管理",
                },
                redirect: "list",
                children: [
                    {
                        path: "list",
                        meta: {
                            hideInMenu: true,
                            title: "渠道经理管理",
                        },
                        component: "@/views/channel/manager/list",
                    },
                    {
                        path: "add",
                        meta: {
                            title: "添加渠道经理",
                            hideInMenu: true,
                        },
                        component: "@/views/channel/manager/details",
                    },
                    {
                        path: "edit",
                        meta: {
                            title: "编辑渠道经理",
                            hideInMenu: true,
                        },
                        component: "@/views/channel/manager/details",
                    },
                ],
            },
            {
                path: "/channel/promoter",
                meta: {
                    title: "地推人员管理",
                    auth: false,
                },
                redirect: "list",
                children: [
                    {
                        path: "/channel/promoter/list",
                        meta: {
                            title: "地推人员管理",
                            hideInMenu: true,
                        },
                        component: "@/views/channel/promoter",
                    },
                    {
                        path: "/channel/promoter/details",
                        meta: {
                            hideInMenu: true,
                        },
                        component: "@/views/channel/promoter/details",
                    },
                ],
            },
        ],
    },
    {
        path: "link",
        meta: {
            title: "外部链接",
            icon: "Link",
        },
        children: [
            {
                link: "https://www.baidu.com",
                meta: {
                    title: "百度",
                    auth: false,
                },
            },
            {
                link: "https://www.google.cn",
                meta: {
                    title: "谷歌",
                },
            },
        ],
    },
    {
        link: "http://www.caih.com",
        meta: {
            title: "东信",
            auth: false,
        },
    },
] as CustomRouteRaw[];

Released under the MIT License.