React+TS前台项目实战(二十二)-- 全局常用导出组件Export封装

文章目录

  • 前言
  • Export组件
    • [1. 功能分析](#1. 功能分析)
    • [2. 代码+详细注释](#2. 代码+详细注释)
    • [3. 使用方式](#3. 使用方式)
    • [4. 效果展示](#4. 效果展示)
  • 总结

前言

今天我们来封装一个带导出图标的导出组件。

Export组件

1. 功能分析

通过传入链接地址,规定要跳转的导出页面,或是直接通过链接导出数据

2. 代码+详细注释

c 复制代码
// @/components/Export/index.tsx
import { useTranslation } from 'react-i18next'
import { Link } from '@/components/Link'
import styles from './styles.module.scss'
import { ReactComponent as ExportIcon } from './export.svg'

/**
 * 导出按钮组件
 * @param {Object} props - 组件属性
 * @param {string} props.link - 导出链接
 * @returns {JSX.Element} - 导出按钮
 */
export function Export({ link }: { link: string }) {
  const [t] = useTranslation()
  return (
    <Link className={styles.exportLink} to={link} target="_blank">
      {/* 按钮文本 */}
      <div>{t(`common.export`)}</div>
      {/* 导出图标 */}
      <ExportIcon />
    </Link>
  )
}
------------------------------------------------------------------------------
// @/components/Export/styles.module.scss
.exportLink {
  height: 50px;
  display: flex;
  align-items: center;
  color: var(--cd-primary-color);
  cursor: pointer;

  > div:first-child {
    margin-right: 8px;
    white-space: nowrap;
  }
}

3. 使用方式

c 复制代码
// 引入组件
import Export from '@/components/Export'
// 使用
<Export link="/export?module=tranction" />

4. 效果展示


总结

下一篇讲【全局常用组件Echarts封装】。关注本栏目,将实时更新。

相关推荐
mCell7 小时前
GSAP ScrollTrigger 详解
前端·javascript·动效
gnip7 小时前
Node.js 子进程:child_process
前端·javascript
excel10 小时前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel11 小时前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼12 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping12 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙13 小时前
[译] Composition in CSS
前端·css
白水清风13 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix13 小时前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户221520442780013 小时前
new、原型和原型链浅析
前端·javascript