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

相关推荐
bigfatDone12 分钟前
OpenSpec + Superpowers 联合开发工作流
前端
北漂大橙子12 分钟前
OpenSpec 完全指南:让 AI 编码可预测的规范框架
前端
lemon_yyds27 分钟前
OpenCode 最佳实践
前端
用户527096487449034 分钟前
前端登录菜单加载性能优化总结
前端
你觉得脆皮鸡好吃吗35 分钟前
Check Anti-CSRF Token (AI)
前端·网络·网络协议·安全·csrf·网络安全学习
一个快乐的咸鱼36 分钟前
nextjs接入AI实现流式输出
前端
誰在花里胡哨1 小时前
Vue<前端页面装修组件>
前端·vue.js
张元清1 小时前
Pareto 动态路由实战:[slug]、catch-all、嵌套布局
前端·javascript·面试
fix一个write十个1 小时前
NativeWind v4 与 React Native UI Kit或三方库样式隔离指南
前端·react native