• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

umi-plugin-menus: 将 umi 生成的 routes 转换成 tree 结构 menus 数据,开发中可直接 ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

umi-plugin-menus

开源软件地址:

https://gitee.com/fattypanda_958/umi-plugin-menus

开源软件介绍:


umi-plugin-menus

NPM versionNPM downloads

routes to menus

umi 生成的 routes 转换成 tree 结构 menus 数据,开发中可直接引入该文件来进行导航菜单的生成

PS: routes 更新时 menus 文件也会实时更新

Usage

Configure in .umirc.js And config/config.js,

.umirc.js

import { join } from 'path';export default {  plugins: [    ['umi-plugin-menus', {  	  build: join(__dirname, './src/layouts/menus.json'),    }],  ],}

src/layouts/index.js

import React, {useState, useEffect} from 'react';import {Layout, Menu} from 'antd';import Link from 'umi/link';import _get from 'lodash/get';import _map from 'lodash/map';import _find from 'lodash/find';import _toString from 'lodash/toString';import _isArray from 'lodash/isArray';import _isEmpty from 'lodash/isEmpty';//  引入插件生成的 routes.json 文件import routes from '../routes.json';const {Content, Sider} = Layout;const menus = _get(routes, [0, 'routes']);/** * 递归生成菜单 * @param {array} menus * @param {object} [parent] * @param {number} [parent.key] * @param {array} stack * @returns {Array} */function recursiveMenus (menus, parent = {}, stack = []) {  const {key: parentKey = ''} = parent;  return _map(menus, (menu, key) => {    const {title, path, routes} = menu;    const k = `${parentKey? `${parentKey}-`: ''}${key}`;    stack.push({key: k, ...menu});    if (_isArray(routes) && !_isEmpty(routes)) {      return (        <Menu.SubMenu key={k} title={title}>          {recursiveMenus(routes, {key}, stack)}        </Menu.SubMenu>      );    } else {      return (        <Menu.Item key={k}>          <Link to={path}>{title}</Link>        </Menu.Item>      );    }  });}function BasicLayout(props) {  const [collapsed, setCollapsed] = useState(false);  const [selectedKeys, setSelectedKeys] = useState(['0']);  const [menuItems, setMenuItems] = useState([]);  const [menuItemsComponent, setMenuItemsComponent] = useState();  useEffect(() => {    const stack = [];    const menuItemsComponent = recursiveMenus(menus, {}, stack);    setMenuItems(stack);    setMenuItemsComponent(menuItemsComponent);  }, []);  useEffect(() => {    const key = _get(_find(menuItems, ({path}) => path === props.location.pathname), ['key']);    if (key) {      setSelectedKeys([_toString(key)]);    }  }, [props.location.pathname]);  return (    <Layout style={{height: '100vh'}}>      <Sider collapsible collapsed={collapsed} onCollapse={v => setCollapsed(v)}>        <Menu theme={'dark'} selectedKeys={selectedKeys} mode={'inline'}>          {menuItemsComponent}        </Menu>      </Sider>      <Layout>        <Content>          <div style={{width: '100vw', height: '100vh'}}>            {props.children}          </div>        </Content>      </Layout>    </Layout>  );}export default BasicLayout;

Options

/** * @param {string} [build=./menus.json] - 导出的路径 * @param {string[]} [excludes=exact,component,Routes] - 返回忽略字段 */export interface options {  build?: string,  excludes?: string[],}

LICENSE

MIT


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
graphic-design: js和vue实现 图形画板发布时间:2022-02-13
下一篇:
吸顶效果: 自定义两种吸顶效果发布时间:2022-02-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap