在后台项目中使用tabs导航是一个重要的功能node
下面介绍一下若是配合umi框架和对应的插件实现此功能react
参考:react-activationgit
参考实现:实现一个tabsgithub
目前项目用的架构设计是umi3+react17+antd pro5npm
插件地址: umi-plugin-keep-alive缓存
npm install umi-plugin-keep-alive --save # or yarn add umi-plugin-keep-alive
components文件夹下建立KeepAvlieTabs
对应的less样式请自行修改本身的需求antd
相关的keepAlive与生命周期使用方案及问题请到上述连接react-activation查看
建立index.tsx文件架构
// /components/KeepAvlieTabs/index.tsx import { useAliveController } from 'react-activation'; import { arrayDeduplicate } from '@/utils/Array'; import type { CachingNode } from './type'; import Tab from './Tab'; import styles from './index.less'; import { useHistory, useLocation } from 'umi'; export default function KeepAliveTabs() { // history导航 const history = useHistory(); // 本地路由信息 const location = useLocation(); // 获取缓存节点方法和信息 const { getCachingNodes } = useAliveController(); const cachingNodes: CachingNode[] = getCachingNodes(); // 由于是异步组件,须要在这儿处理一下缓存中的重复问题 let nodes: CachingNode[] = arrayDeduplicate(cachingNodes, 'path'); // 首页不参与tabs切换的关闭操做 nodes = nodes.filter((item) => item.path !== '/home'); return ( <ul className={styles['alive-tabs']}> <li className={location.pathname === '/home' ? styles.home_active : styles.home_deactive} onClick={() => { history.push('/home'); }} > <div className="tags-nav"> <span>首页</span> </div> </li> {nodes.map((node) => ( <Tab key={node!.id} node={node} /> ))} </ul> ); }
建立Tab.tsx文件app
// /components/KeepAvlieTabs/Tab.tsx import { useHistory, useLocation } from 'umi'; import { useAliveController } from 'react-activation'; import { CloseCircleOutlined } from '@ant-design/icons'; import type { CachingNode } from './type'; import styles from './index.less'; export default function Tab({ node }: { node: CachingNode }) { const history = useHistory(); const location = useLocation(); // 同上,dropScope是释放节点,点删除后删掉当前节点信息 const { getCachingNodes, dropScope } = useAliveController(); const cachingNodes: CachingNode[] | any[] = getCachingNodes(); // 执行tab的删除操做 function dropTab(e: React.MouseEvent<HTMLButtonElement>) { e.stopPropagation(); // 若是关闭激活中的 KeepAlive Tab,须要先离开当前路由 // 触发 KeepAlive unactivated 后再进行 drop if (location.pathname === node.path) { // 路由异步加载控制 const unlisten = history.listen(() => { setTimeout(() => { dropScope(node.name as string | RegExp); }, 30); }); unlisten(); // 前往排除当前 node 后的最后一个 tab if (cachingNodes.length <= 1) { history.push('/'); } else { const { path } = cachingNodes.filter((item) => item.path !== node.path).pop(); history.push(path); } } else { dropScope(node.name as string | RegExp); } } // 设置当前tab的样式 const className = () => { if (location.pathname === node.path) { if (location.pathname === '/home') { return `${styles.active} ${styles.home_active}`; } return `${styles.active}`; } return `${styles.deactive}`; }; return ( <li className={className()} onClick={() => { history.push(node.path); }} > <div className="tags-nav"> <span>{node.name}</span> {<CloseCircleOutlined className={styles['close-btn']} onClick={dropTab} />} </div> </li> ); }
建立类型文件type.ts框架
export type CachingNode = { createTime: number; updateTime: number; name?: string; id: string; [key: string]: any; };
建立样式less文件
.alive-tabs { height: 100%; margin: 0; padding: 0; overflow: hidden; white-space: nowrap; background: #fff; li { position: relative; display: inline-block; padding: 0 28px 0 10px; line-height: 32px; vertical-align: top; list-style: none; border-right: solid 1px #e6e6e6; cursor: pointer; transition: background 0.2s; } .active { height: 32px; background: rgba(#1890ff, 0.1); border-bottom: 2px solid #1890ff; // background-color: #42b983; } .home_active { height: 32px; padding: 0 10px; background: rgba(#1890ff, 0.1); border-bottom: 2px solid #1890ff; // background-color: #42b983; } .home_deactive { padding: 0 10px; } .deactive { background: #fff; } .close-btn { position: absolute; top: 11px; right: 10px; display: flex; align-items: center; justify-content: center; font-size: 12px; line-height: 0; outline: none; // transform: translateY(-50%); } }
建立Books/index.tsx
import React from 'react'; import { KeepAlive, useActivate, useUnactivate } from 'react-activation'; const Books: React.FC = () => { useActivate(()=>{ console.log("我被激活了"); }) useUnactivate(()=>{ console.log("我被缓存了"); }) return <div>我是被缓存的组件</div>; }; export default (props: any) => { // 下面使用这样的方式去控制路由操做 // 是为了同步显示路由定义中的title和匹配路由path作对应的操做 // 可根据自行需求修改当前的逻辑 return ( <KeepAlive name={props.route.name} path={props.route.path} saveScrollPosition="screen"> <Books /> </KeepAlive> ); };
咱们的页面布局左上下结构,在app.tsx中,挂载到咱们的头部中,超出显示滚动条,具体的样式和功能请自行添加
未实现鼠标右键菜单关闭全部,左右切换,请自行添加,antd有相关的组件
export const layout: RunTimeLayoutConfig = ({ initialState }: any) => { return { // 控制组件是否可缓存 disableMobile: true, headerContentRender: () => <KeepAliveTabs />, ... }; };