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: dynamic menus #60

Merged
merged 10 commits into from
Apr 28, 2022
Merged

feat: dynamic menus #60

merged 10 commits into from
Apr 28, 2022

Conversation

alexzhang1030
Copy link
Collaborator

@alexzhang1030 alexzhang1030 commented Apr 27, 2022

Description

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).

About This PR

close #51

目前已实现动态读取 routes/modules/xx.ts 中的路由,并自动渲染视图

feat

interface RouteMeta {
    // 标题
    title: string
    // 是否是菜单
    isMenu: boolean
    // icon
    icon?: FunctionalComponent
    // 是否是单独的菜单
    single?: boolean
    // 排序索引
    sort: number
  }

以上内容需要放在 route 定义的 meta 字段中,例如

{
    path: 'fullscreen',
    name: 'fullscreen',
    component: () => import('~/views/demo/fullscreen/index.vue'),
    meta: {
      title: '全屏',
      isMenu: true,
      sort: 8
    }
}

根据 children 自动适配多级菜单,若子 route 有 children,那么说明该路由是一个父级菜单,可展开。
理论上可以自动适配任意多级的菜单

实现原理

读取 routes,并 v-for 渲染,因此一定要配置好 meta 字段中的内容

关于文档,将会在 merge 后去改

自动适配多级菜单的原理就是写了两个组件,一个是单独的,一个是含有子级的,根据其 children 递归渲染

修改较多,review 可能比较困难,辛苦大家了!

@alexzhang1030 alexzhang1030 changed the title feat: dynamic routes feat: dynamic menus Apr 27, 2022
zgsgs
zgsgs approved these changes Apr 27, 2022
@architectMymine architectMymine removed their request for review April 27, 2022 13:20
Copy link
Member

@RainyNight9 RainyNight9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有2个问题:
1、// 排序索引 sort: number 是可选参数吗? 注意顺序
2、整体代码有没有 lint ?

@alexzhang1030
Copy link
Collaborator Author

有2个问题: 1、// 排序索引 sort: number 是可选参数吗? 注意顺序 2、整体代码有没有 lint ?

  1. sort 这个我改一下🥲
  2. 代码 lint 过了

@alexzhang1030
Copy link
Collaborator Author

目前 route.meta 的参数肯定是不足的,看后期还要加什么

@netlify
Copy link

netlify bot commented Apr 27, 2022

Deploy Preview for vue-hbs-admin ready!

Name Link
🔨 Latest commit 2f7d5d3
🔍 Latest deploy log https://app.netlify.com/sites/vue-hbs-admin/deploys/62697b6559dcb100085812d8
😎 Deploy Preview https://deploy-preview-60--vue-hbs-admin.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@alexzhang1030
Copy link
Collaborator Author

修改好了!

v-model:selectedKeys="selectedKeys"
mode="inline"
theme="dark"
v-model:openKeys="openKeys" v-model:selectedKeys="selectedKeys" mode="inline" theme="dark"
:inline-collapsed="getCollapsed"
>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

属性较长建议换行,可读性要高一些

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看到类似这个,所以我怀疑 有没有 lint ..

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

非常尴尬的是,我确实运行了 pnpm lint -- --fix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lint 有时候并不能很好的去规避所有的格式问题,只能起到一个辅助的作用。一般我都更喜欢自己手动去做这件事,所以之前没有在 lint 的时候,加上 --fix。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

学到了

const currentRoute = router.currentRoute.value
selectedKeys.value = getCurrentMenuRecursive(menus.value, currentRoute.path)
openKeys.value = selectedKeys.value
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setSelectMenuWhenRefreshPage 这个命名更像是解决了什么问题。可以简单点,比如:setupCurrentMenu ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数主要是解决刷新后当前路由对应左侧菜单的

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我无法分辨哪个名字更好,你觉得呢

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实我也不咋会起名字。不过要我感觉,setupCurrentMenu 这个加上你上面写的注释就很完美了

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的!

@@ -1,11 +1,21 @@
import type { RouteRecordRaw } from 'vue-router'
import {
SettingOutlined
} from '@ant-design/icons-vue'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

单个不必折行

single?: boolean
// 排序索引
sort: number
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. single 字段是不是可有可无?如果可无,是否可以舍去?
  2. 当存在 3 级导航,isMenu 还好用吗,或者说用一个 type: 1 | 2 | 3 替代会好一些?
  3. sort 字段我感觉只有一级导航才需要,使用动态导入顺序不可控。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. single 的作用是标识一下当前的一级菜单就是一个 menuItem,没有二级菜单
  2. 这个我要思考一下,可能当时没考虑到三级菜单我要实验一下
  3. 我有用 sort 递归排序所有的路由,所以 sort 的顺序我觉得还是有用的

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要说明一下 2 的 isMenu 是 false 的话,是不会展示的,主要是这个作用,不过倒是提醒了我三级导航的问题 @Hongbusi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

果然,我没有完全读懂哈哈

@Hongbusi
Copy link
Member

image
单词拼错了哈哈

@alexzhang1030
Copy link
Collaborator Author

image 单词拼错了哈哈

打的有点快🥲

@alexzhang1030
Copy link
Collaborator Author

@Hongbusi @RainyNight9 重大更新,目前已适配多级菜单,理论上 99 菜单都会自动适配

@alexzhang1030
Copy link
Collaborator Author

alexzhang1030 commented Apr 27, 2022

isMenu 更改为 isHide,更加语义化,同时该选项非必填,加了筛选 isHide 的路由的函数

@Hongbusi
Copy link
Member

文档记得更新一下哦~

@alexzhang1030
Copy link
Collaborator Author

文档记得更新一下哦~

等合并后😊

@Hongbusi Hongbusi merged commit 768e7f8 into developer-plus:master Apr 28, 2022
@alexzhang1030 alexzhang1030 deleted the dynamic_routes branch April 28, 2022 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

动态读取路由,并渲染左侧菜单
4 participants