Umi3.5配置全局路由和菜单
目录
需求分析
但是根据我们的使用经验,还有一个功能缺席:
全局菜单显示,点击跳转到不同的页面
实现效果如下所示:
功能实现
1.构建全局layout页面并实现路由跳转
src/文件夹下新建layouts文件夹及index.js文件。
修改index.js如下:
-
export default withRouter(({ children, location }) => {
-
-
return (
-
<div>
-
-
<div>
-
<Switch location={location}>{children.props.children}</Switch>
-
</div>
-
</div>
-
);
-
});
修改.umirc.ts文件如下:
-
import { defineConfig } from 'umi';
-
-
export default defineConfig({
-
nodeModulesTransform: { //设置 node_modules 目录下依赖文件的编译方式
-
type: 'none',
-
},
-
//umi 的路由基于 react-router@5 实现,配置和 react-router 基本一致
-
-
routes: [
-
{
-
path: '/',
-
component: '@/layouts/index',
-
routes: [
-
{
-
exact: true,
-
path: '/Demo1',
-
name: 'Demo1',
-
component: '@/pages/Demo1/index',
-
icon: 'SettingOutlined',
-
},
-
{
-
exact: true,
-
path: '/Demo2',
-
name: 'Demo2',
-
component: '@/pages/Demo2/index',
-
icon: 'AppstoreOutlined',
-
},
-
],
-
},
-
],
-
-
fastRefresh: {}, //快速刷新(Fast Refresh),开发时可以保持组件状态,同时编辑提供即时反馈
-
ignoreMomentLocale: true, //忽略 moment 的 locale 文件,用于减少尺寸
-
hash: true, //配置是否让生成的文件包含 hash 后缀,通常用于增量发布和避免浏览器加载缓存
-
targets: { //配置需要兼容的浏览器最低版本,会自动引入 polyfill 和做语法转换
-
ie: 9,
-
},
-
dva: {
-
immer: true,
-
hmr: false,
-
},
-
});
综合以上两部分,我们可实现全局路由调整,UmiJs在调整时首先1载入 @layouts/index 组件,然后再调整到具体的路由载入另一个页面。
实现全局路由跳转之后,问题又来了——目前我们只能手动输入路由才能实现跳转,因此我们需在layout中加入一个菜单便于跳转。
2.全局layout根据路由配置生成具体菜单
此处菜单我们使用Antd的Menu组件实现。
如下图所示:
项目根目录下创建config文件夹,config文件夹下创建 router.config.js 文件,修改内容如下:
-
const routerConfig = [
-
{
-
path: '/',
-
component: '@/layouts/index',
-
routes: [
-
{
-
exact: true,
-
path: '/Demo1',
-
name: 'Demo1',
-
component: '@/pages/Demo1/index',
-
icon: 'SettingOutlined',
-
},
-
{
-
exact: true,
-
path: '/Demo2',
-
name: 'Demo2',
-
component: '@/pages/Demo2/index',
-
icon: 'AppstoreOutlined',
-
},
-
],
-
},
-
];
-
export default routerConfig;
根据路由配置信息,再结合Menu组件参数配置实现动态生成。
修改layouts/index.js文件如以下内容:
-
import React, { Fragment, useState } from 'react';
-
import { withRouter, Switch, history } from 'umi';
-
import { Menu } from 'antd';
-
import * as Icon from '@ant-design/icons';
-
import routerConfig from '../../config/router.config.js';
-
-
const { SubMenu } = Menu;
-
-
const getIcon = (iconName) => {
-
const res = React.createElement(Icon[iconName], {
-
style: { fontSize: '16px' },
-
});
-
return res;
-
};
-
-
const getSubMenu = (routesData) => {
-
routesData.map((item) => {
-
return <Menu.Item key={item.path}>{item.name}</Menu.Item>;
-
});
-
};
-
// 实现菜单构建函数
-
const getMenu = (routesData) => {
-
const menuData = [];
-
for (let i = 0; i < routesData.length; i = 1) {
-
if (Object.prototype.hasOwnProperty.call(routesData[i], 'routes')) {
-
menuData.push(
-
<SubMenu
-
key={routesData[i].path}
-
title={routesData[i].name}
-
icon={getIcon(routesData[i].icon)}
-
>
-
{getSubMenu(routesData[i].routes)}
-
</SubMenu>,
-
);
-
} else {
-
menuData.push(
-
<Menu.Item key={routesData[i].path} icon={getIcon(routesData[i].icon)}>
-
{routesData[i].name}
-
</Menu.Item>,
-
);
-
}
-
}
-
return menuData;
-
};
-
const CreateMenu = () => {
-
const [levelOne] = routerConfig;
-
const { routes } = levelOne;
-
return <Fragment>{getMenu(routes)}</Fragment>;
-
};
-
-
export default withRouter(({ children, location }) => {
-
const [current, setCurrent] = useState('');
-
const handleClick = (e) => {
-
history.push(e.key);
-
setCurrent(e.key);
-
};
-
-
return (
-
<div>
-
<Menu onClick={handleClick} selectedKeys={[current]} mode="horizontal">
-
{CreateMenu()}
-
</Menu>
-
<div>
-
<Switch location={location}>{children.props.children}</Switch>
-
</div>
-
</div>
-
);
-
});
项目Gitee地址
以上,就是本次配置的全部内容~
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgbegjh
系列文章
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01