# 18. Icon 图标
URL: /%E6%96%87%E6%A1%A3/04.%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/01.%20%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/03.%20%E7%BB%84%E4%BB%B6%E5%BA%93%E6%96%87%E6%A1%A3/09.%20%E5%85%B6%E4%BB%96%E7%BB%84%E4%BB%B6/20.%20Icon%20%E5%9B%BE%E6%A0%87

Icon 图标组件使用指南


# Icon 图标组件

> [!info] 基于 @iconify/vue 的图标组件，支持在线图标库和自定义 SVG 图标。

## 基本用法

```vue
<template>
  <Icon icon="ant-design:home-outlined" />
  <Icon icon="mdi:account" />
  <Icon icon="logos:vue" />
</template>

<script setup lang="ts">
import { Icon } from '@/components/Icon';
</script>
```

## 常用图标库

| 图标集 | 前缀 | 示例 |
|--------|------|------|
| Ant Design | `ant-design` | `ant-design:home-outlined` |
| Material Design Icons | `mdi` | `mdi:account` |
| FontAwesome | `fa` | `fa:user` |
| Logos | `logos` | `logos:vue` |
| Carbon | `carbon` | `carbon:dashboard` |

```vue
<!-- Ant Design Icons -->
<Icon icon="ant-design:home-outlined" />
<Icon icon="ant-design:user-outlined" />
<Icon icon="ant-design:setting-outlined" />

<!-- Material Design Icons -->
<Icon icon="mdi:home" />
<Icon icon="mdi:account-circle" />
<Icon icon="mdi:cog" />

<!-- Logos -->
<Icon icon="logos:vue" />
<Icon icon="logos:react" />
<Icon icon="logos:google" />
```

## Props 属性

| 属性 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| icon | `string` | `undefined` | 图标名称，支持 Iconify 格式（如 `ant-design:home-outlined`）或以 `|svg` 结尾使用本地 SVG |
| size | `number \| string` | `16` | 图标大小（像素） |
| color | `string` | `undefined` | 图标颜色，支持颜色名、十六进制、RGB 等 |
| spin | `boolean` | `false` | 是否旋转动画 |
| prefix | `string` | `''` | 图标前缀 |

### SvgIcon Props

| 属性 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| name | `string` | - | SVG 图标名称（必填） |
| size | `number \| string` | `16` | 图标大小（像素） |
| spin | `boolean` | `false` | 是否旋转动画 |
| prefix | `string` | `'icon'` | SVG 类名前缀 |

### IconPicker Props

| 属性 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| value | `string` | - | 选中的图标值（v-model） |
| width | `string` | `'100%'` | 输入框宽度 |
| copy | `boolean` | `false` | 是否支持复制 |
| mode | `'svg' \| 'iconify'` | `'iconify'` | 图标模式 |
| disabled | `boolean` | `false` | 是否禁用 |
| clearSelect | `boolean` | `false` | 是否可清空选择 |
| iconPrefixSave | `boolean` | `true` | 保存时是否包含图标前缀 |

### IconPicker Emits

| 事件 | 参数 | 说明 |
|------|------|------|
| update:value | `(value: string)` | 图标值变化时触发 |
| change | `(value: string)` | 图标选择变化时触发 |

## 大小与颜色

```vue
<template>
  <!-- 设置大小 -->
  <Icon icon="ant-design:home-outlined" :size="20" />
  <Icon icon="ant-design:home-outlined" :size="32" />
  <Icon icon="ant-design:home-outlined" size="48px" />

  <!-- 设置颜色 -->
  <Icon icon="ant-design:home-outlined" color="#1890ff" />
  <Icon icon="ant-design:home-outlined" color="red" />

  <!-- 同时设置 -->
  <Icon icon="ant-design:home-outlined" :size="24" color="#1890ff" />
</template>
```

## 旋转动画

```vue
<template>
  <Icon icon="ant-design:loading-outlined" :spin="true" />
</template>
```

## 自定义 SVG 图标

使用 SvgIcon 组件加载本地 SVG（图标名称需以 `|svg` 结尾）：

```vue
<template>
  <!-- 方式一：自动识别本地 SVG -->
  <Icon icon="logo|svg" :size="32" />

  <!-- 方式二：直接使用 SvgIcon 组件 -->
  <SvgIcon name="logo" :size="32" />
</template>

<script setup lang="ts">
import { Icon, SvgIcon } from '@/components/Icon';
</script>
```

## 在按钮中使用

```vue
<template>
  <a-button>
    <Icon icon="ant-design:plus-outlined" />
    新增
  </a-button>

  <a-button>
    <Icon icon="ant-design:edit-outlined" />
    编辑
  </a-button>

  <a-button danger>
    <Icon icon="ant-design:delete-outlined" />
    删除
  </a-button>
</template>
```

## 动态图标

```vue
<template>
  <Icon :icon="currentIcon" />
  <a-button @click="toggleIcon">切换图标</a-button>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Icon } from '@/components/Icon';

const currentIcon = ref('ant-design:home-outlined');

function toggleIcon() {
  currentIcon.value =
    currentIcon.value === 'ant-design:home-outlined'
      ? 'ant-design:user-outlined'
      : 'ant-design:home-outlined';
}
</script>
```

## 图标选择器

使用 IconPicker 选择图标：

```vue
<template>
  <IconPicker v-model:value="icon" />
  <Icon :icon="icon" :size="32" />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { IconPicker, Icon } from '@/components/Icon';

const icon = ref('ant-design:home-outlined');
</script>
```

## 在表格中使用

```vue
<template>
  <BasicTable @register="registerTable" />
</template>

<script setup lang="ts">
import { h } from 'vue';
import { Icon } from '@/components/Icon';
import { BasicTable, useTable } from '@/components/Table';

const columns = [
  {
    title: '操作',
    dataIndex: 'action',
    customRender: ({ record }) => [
      h(Icon, {
        icon: 'ant-design:edit-outlined',
        style: { cursor: 'pointer', marginRight: '8px' },
        onClick: () => handleEdit(record),
      }),
      h(Icon, {
        icon: 'ant-design:delete-outlined',
        style: { cursor: 'pointer', color: 'red' },
        onClick: () => handleDelete(record),
      }),
    ],
  },
];

const [registerTable] = useTable({ columns });

function handleEdit(record) {
  console.log('编辑', record);
}

function handleDelete(record) {
  console.log('删除', record);
}
</script>
```

## 常用图标列表

### 导航类

```vue
<!-- 首页 -->
<Icon icon="ant-design:home-outlined" />

<!-- 用户 -->
<Icon icon="ant-design:user-outlined" />

<!-- 设置 -->
<Icon icon="ant-design:setting-outlined" />

<!-- 退出 -->
<Icon icon="ant-design:logout-outlined" />
```

### 操作类

```vue
<!-- 新增 -->
<Icon icon="ant-design:plus-outlined" />

<!-- 编辑 -->
<Icon icon="ant-design:edit-outlined" />

<!-- 删除 -->
<Icon icon="ant-design:delete-outlined" />

<!-- 搜索 -->
<Icon icon="ant-design:search-outlined" />

<!-- 刷新 -->
<Icon icon="ant-design:reload-outlined" />

<!-- 下载 -->
<Icon icon="ant-design:download-outlined" />
```

### 状态类

```vue
<!-- 成功 -->
<Icon icon="ant-design:check-circle-outlined" style="color: #52c41a" />

<!-- 错误 -->
<Icon icon="ant-design:close-circle-outlined" style="color: #ff4d4f" />

<!-- 警告 -->
<Icon icon="ant-design:exclamation-circle-outlined" style="color: #faad14" />

<!-- 信息 -->
<Icon icon="ant-design:info-circle-outlined" style="color: #1890ff" />
```

## 在线图标搜索

访问 [Iconify](https://icon-sets.iconify.design/) 搜索和预览图标。

## 相关文件

- 组件路径：`@/components/Icon/src/Icon.vue`
- SvgIcon：`@/components/Icon/src/SvgIcon.vue`
- IconPicker：`@/components/Icon/src/IconPicker.vue`

## 相关文档

- [[01. 前端规范/07. 图标选择器]] - 图标选择器详细说明
