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封装】。关注本栏目,将实时更新。

相关推荐
二哈喇子!7 小时前
BOM模型
开发语言·前端·javascript·bom
二哈喇子!7 小时前
Vue2 监听器 watcher
前端·javascript·vue.js
摘星编程7 小时前
OpenHarmony + RN:Bluetooth连接蓝牙外设
react native·react.js·harmonyos
yanyu-yaya8 小时前
前端面试题
前端·面试·前端框架
二哈喇子!8 小时前
使用NVM下载Node.js管理多版本
前端·npm·node.js
GGGG寄了8 小时前
HTML——文本标签
开发语言·前端·html
摘星编程9 小时前
在OpenHarmony上用React Native:ActionSheet确认删除
javascript·react native·react.js
2501_944521599 小时前
Flutter for OpenHarmony 微动漫App实战:推荐动漫实现
android·开发语言·前端·javascript·flutter·ecmascript
Amumu1213810 小时前
Vue核心(三)
前端·javascript·vue.js
CoCo的编程之路10 小时前
2026 前端效能革命:如何利用智能助手实现“光速”页面构建?深度横评
前端·人工智能·ai编程·comate·智能编程助手·文心快码baiducomate