Skip to content

图标使用

介绍

项目提供强大灵活的图标系统,支持三种图标类型:内置字体图标UnoCSS 图标图片图标

核心特性:

  • 内置字体图标 - 200+ 矢量图标,11 个类别,支持离线
  • UnoCSS 图标 - 集成 Iconify,支持数万图标
  • 图片图标 - 支持本地和网络图片
  • 样式定制 - 自定义颜色、尺寸、动画
  • 类型安全 - 完整 TypeScript 类型定义

图标类型

内置字体图标

基于 iconfont 字体实现,风格统一:

vue
<template>
  <view class="icon-demo">
    <wd-icon name="home" />
    <wd-icon name="search" />
    <wd-icon name="setting" />
    <!-- 填充版:名称后加 -fill -->
    <wd-icon name="home-fill" />
  </view>
</template>

UnoCSS 图标

使用 Iconify 图标库,以 i- 为前缀:

vue
<template>
  <view class="icon-demo">
    <!-- Element Plus -->
    <wd-icon name="i-ep-user" />
    <wd-icon name="i-ep-setting" />
    <!-- Carbon -->
    <wd-icon name="i-carbon-home" />
    <!-- Material Design -->
    <wd-icon name="i-mdi-account" />
  </view>
</template>

常用图标集:

前缀图标集说明
i-ep-Element PlusElement Plus 设计图标
i-carbon-CarbonIBM Carbon 设计图标
i-mdi-Material Design IconsGoogle Material 图标
i-tabler-Tabler Icons简洁线性图标
i-ri-Remix Icon中性风格图标
i-lucide-LucideFeather 扩展版

图片图标

使用本地或网络图片作为图标:

vue
<template>
  <view class="icon-demo">
    <wd-icon name="/static/icons/custom.png" size="32" />
    <wd-icon name="https://example.com/icon.png" size="32" />
  </view>
</template>

图标属性

尺寸设置

通过 size 属性设置大小:

vue
<template>
  <view class="icon-demo">
    <wd-icon name="user" size="24" />
    <wd-icon name="user" size="32" />
    <wd-icon name="user" size="48" />
    <wd-icon name="user" size="16px" />
  </view>
</template>

尺寸规范:

场景推荐尺寸
按钮图标32-40
列表图标40-48
导航图标44-52
大图标64-96

颜色设置

通过 color 属性设置颜色:

vue
<template>
  <view class="icon-demo">
    <wd-icon name="heart" color="#1989fa" />
    <wd-icon name="heart" color="#07c160" />
    <wd-icon name="heart" color="var(--wd-color-primary)" />
  </view>
</template>

主题颜色变量:

变量说明默认值
--wd-color-primary主题色#1989fa
--wd-color-success成功色#07c160
--wd-color-warning警告色#ff976a
--wd-color-danger危险色#ee0a24

旋转动画

为图标添加旋转动画:

vue
<template>
  <wd-icon name="loading" custom-class="rotating" />
</template>

<style lang="scss" scoped>
.rotating {
  animation: rotate 1s linear infinite;
}
@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
</style>

内置图标分类

系统操作类

图标名填充版说明
homehome-fill首页
backback-fill返回
closeclose-fill关闭
checkcheck-fill确认
menumenu-fill菜单
moremore-fill更多
searchsearch-fill搜索
filterfilter-fill筛选
refreshrefresh-fill刷新
settingsetting-fill设置
helphelp-fill帮助
addadd-fill添加
editedit-fill编辑
deletedelete-fill删除
copycopy-fill复制
savesave-fill保存
uploadupload-fill上传
downloaddownload-fill下载
shareshare-fill分享
scanscan-fill扫描
playplay-fill播放
pausepause-fill暂停

导航箭头类

图标名说明
up / down / left / right方向箭头
chevron-up/down/left/rightV形箭头
caret-up/down/left/right三角箭头
backtop返回顶部
unfold-more / unfold-less展开/收起

状态指示类

图标名填充版说明
warnwarn-fill警告
infoinfo-fill信息
loading-加载中
lockedlocked-fill已锁定
visiblevisible-fill可见
hiddenhidden-fill隐藏
starstar-fill星标
heartheart-fill爱心
likelike-fill点赞
notificationnotification-fill通知

用户相关类

图标名填充版说明
useruser-fill用户
user-adduser-add-fill添加用户
teamteam-fill团队
contactcontact-fill联系人
vipvip-fillVIP
malemale-fill男性
femalefemale-fill女性
crowncrown-fill皇冠

通讯媒体类

图标名填充版说明
callcall-fill通话
phonephone-fill电话
messagemessage-fill消息
mailmail-fill邮件
cameracamera-fill相机
imageimage-fill图片
wifiwifi-fillWiFi
cloudcloud-fill

商业功能类

图标名填充版说明
cartcart-fill购物车
paymentpayment-fill支付
orderorder-fill订单
couponcoupon-fill优惠券
giftgift-fill礼物
walletwallet-fill钱包
shopshop-fill商店
goodsgoods-fill商品
qrcodeqrcode-fill二维码

文件管理类

图标名填充版说明
filefile-fill文件
folderfolder-fill文件夹
file-wordfile-word-fillWord
file-excelfile-excel-fillExcel
file-pptfile-ppt-fillPPT
file-pdffile-pdf-fillPDF
attachattach-fill附件

工具功能类

图标名填充版说明
calendarcalendar-fill日历
locationlocation-fill位置
mapmap-fill地图
toolstools-fill工具
historyhistory-fill历史
serviceservice-fill客服

数据图表类

图标名说明
chart / chart-fill折线图
chart-bar / chart-bar-fill柱状图
trending-up上升趋势
trending-down下降趋势
asc / desc升序/降序

平台品牌类

图标名填充版说明
wechatwechat-fill微信
qqqq-fillQQ
weiboweibo-fill微博
alipayalipay-fill支付宝
githubgithub-fillGitHub
appleapple-fillApple

图标与组件结合

图标按钮

vue
<template>
  <wd-button icon="add">新增</wd-button>
  <wd-button icon="edit" type="primary">编辑</wd-button>
  <wd-button icon="search" round />
</template>

输入框图标

vue
<template>
  <wd-input v-model="username" placeholder="请输入用户名">
    <template #prefix>
      <wd-icon name="user" size="36" color="#999" />
    </template>
  </wd-input>

  <wd-input v-model="password" :type="showPwd ? 'text' : 'password'">
    <template #suffix>
      <wd-icon
        :name="showPwd ? 'visible' : 'hidden'"
        @click="showPwd = !showPwd"
      />
    </template>
  </wd-input>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
const username = ref('')
const password = ref('')
const showPwd = ref(false)
</script>

列表项图标

vue
<template>
  <wd-cell-group>
    <wd-cell title="个人信息" is-link>
      <template #icon>
        <wd-icon name="user-fill" size="40" color="#1989fa" />
      </template>
    </wd-cell>
    <wd-cell title="账户安全" is-link>
      <template #icon>
        <wd-icon name="secured-fill" size="40" color="#07c160" />
      </template>
    </wd-cell>
  </wd-cell-group>
</template>

状态图标

vue
<template>
  <!-- 收藏状态 -->
  <view @click="favorited = !favorited">
    <wd-icon
      :name="favorited ? 'star-fill' : 'star'"
      :color="favorited ? '#ffc107' : '#999'"
      size="48"
    />
  </view>

  <!-- 点赞状态 -->
  <view @click="liked = !liked">
    <wd-icon
      :name="liked ? 'heart-fill' : 'heart'"
      :color="liked ? '#ee0a24' : '#999'"
      size="48"
    />
  </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
const favorited = ref(false)
const liked = ref(false)
</script>

API

Props

参数说明类型默认值
name图标名称string-
color图标颜色string-
size图标大小(默认rpx)string | number40
class-prefix类名前缀stringwd-icon
custom-style自定义样式string-
custom-class自定义类名string-

Events

事件名说明回调参数
click点击时触发event: Event
touch触摸时触发event: Event

类型定义

typescript
/** 字体图标名称 */
type FontIconName =
  | 'home' | 'home-fill'
  | 'back' | 'back-fill'
  | 'close' | 'close-fill'
  | 'check' | 'check-fill'
  | 'search' | 'search-fill'
  | 'setting' | 'setting-fill'
  | 'user' | 'user-fill'
  // ... 更多图标

/** UnoCSS 图标名称 */
type UnoIconName = `i-${string}`

/** 图标名称联合类型 */
type IconName = FontIconName | UnoIconName | string

/** 图标组件属性 */
interface WdIconProps {
  name: IconName
  color?: string
  size?: string | number
  classPrefix?: string
  customStyle?: string
  customClass?: string
}

主题定制

CSS 变量

scss
$wd-icon-size: 40rpx !default;
$wd-icon-color: inherit !default;

自定义图标字体

vue
<template>
  <wd-icon name="custom-home" class-prefix="my-icon" size="40" />
</template>

<style lang="scss">
@font-face {
  font-family: 'my-icons';
  src: url('/static/fonts/my-icons.ttf') format('truetype');
}
.my-icon {
  font-family: 'my-icons' !important;
}
.my-icon-custom-home:before {
  content: '\e001';
}
</style>

最佳实践

1. 选择合适的图标类型

vue
<template>
  <!-- 常用功能:内置字体图标 -->
  <wd-icon name="home" />

  <!-- 特殊图标:UnoCSS -->
  <wd-icon name="i-ep-document" />

  <!-- 品牌Logo:图片 -->
  <wd-icon name="/static/icons/brand.svg" />
</template>

2. 保持一致性

vue
<template>
  <!-- 同一行图标使用相同尺寸 -->
  <view class="action-bar">
    <wd-icon name="like" size="40" />
    <wd-icon name="comment" size="40" />
    <wd-icon name="share" size="40" />
  </view>
</template>

3. 语义化颜色

vue
<template>
  <wd-icon name="check-outline-fill" color="#07c160" /> <!-- 成功 -->
  <wd-icon name="warn-fill" color="#ff976a" />          <!-- 警告 -->
  <wd-icon name="close-outline-fill" color="#ee0a24" /> <!-- 错误 -->
</template>

4. 点击反馈

vue
<template>
  <view class="icon-btn" @click="handleClick">
    <wd-icon name="star" size="48" />
  </view>
</template>

<style lang="scss" scoped>
.icon-btn {
  padding: 16rpx;
  border-radius: 50%;
  &:active {
    background-color: rgba(0, 0, 0, 0.05);
  }
}
</style>

常见问题

1. 图标不显示

原因: 图标名称错误或字体未加载

解决:

vue
<template>
  <wd-icon name="home" />      <!-- 正确 -->
  <wd-icon name="Home" />      <!-- 错误:大小写敏感 -->
  <wd-icon name="i-ep-user" /> <!-- 正确:UnoCSS需要i-前缀 -->
</template>

2. 图标颜色不变

原因: 图片图标不受 color 属性影响

解决:

vue
<template>
  <!-- 字体图标:直接使用 color -->
  <wd-icon name="user" color="#1989fa" />

  <!-- 图片图标:使用 CSS filter -->
  <wd-icon
    name="/static/icons/user.png"
    custom-style="filter: invert(40%) sepia(90%) saturate(1000%);"
  />
</template>

3. UnoCSS 图标打包丢失

原因: 动态图标名称无法静态分析

解决:

typescript
// uno.config.ts
export default defineConfig({
  safelist: [
    'i-ep-user',
    'i-ep-setting',
    // 添加需要的图标
  ],
})

4. 图标点击区域小

解决:

vue
<template>
  <view class="icon-wrapper" @click="handleClick">
    <wd-icon name="close" size="32" />
  </view>
</template>

<style lang="scss" scoped>
.icon-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 88rpx;
  height: 88rpx;
}
</style>

5. 平台显示差异

vue
<script lang="ts" setup>
import { computed } from 'vue'

const iconSize = computed(() => {
  // #ifdef H5
  return 20
  // #endif
  // #ifdef MP-WEIXIN || APP-PLUS
  return 40
  // #endif
})
</script>