@@ -3,88 +3,67 @@ title: 图标
3
3
icon : i-twemoji-beaming-face-with-smiling-eyes
4
4
---
5
5
6
- 图标是前端的灵魂,你可以直接使用官方的 ` @iconify/vue ` 组件来引入在线图标。或者使用项目封装的 ` <AdminIcon /> ` 组件来使用离线打包进项目的 svg 和 iconify 图标。也可以直接使用 ` unocss ` 提供的图标预设。此外,项目还封装了 ` <AdminIconOnline /> ` 组件来使用在线图标 。
6
+ 图标是前端的灵魂,你可以直接使用官方的 ` @iconify/vue ` 组件来引入在线图标。或者使用项目封装的 ` <Icon /> ` 组件来使用离线打包进项目的 svg 和 iconify 图标。也可以直接使用 ` unocss ` 提供的图标预设。
7
7
8
- ## 使用` @iconify/vue ` 组件来引入在线图标
8
+ ## 使用 ` <Icon /> ` 图标组件
9
9
10
- 1 . 在入口页面 ` app.vue ` 中初始化在线图标的 api 服务地址
11
- ``` ts
12
- import { addAPIProvider , disableCache } from ' @iconify/vue'
13
- addAPIProvider (' ' , {
14
- resources: [' https://api.iconify.design' ],
15
- })
16
- disableCache (' all' )
17
- ```
18
-
19
- 2 . 在页面中引用图标组件
20
- ``` vue
21
- <script setup lang="ts">
22
- import { Icon as IconifyIcon } from '@iconify/vue'
23
- </script>
24
-
25
- <template>
26
- <IconifyIcon icon="ep:element-plus" />
27
- </template>
28
- ```
29
-
30
- ## 直接使用 ` <AdminIcon /> ` 图标组件
31
-
32
- 项目注册了可以全局使用的 ` <AdminIcon /> ` 图标组件。
10
+ 项目封装了 ` <Icon /> ` 图标组件来方便图标的引用。
33
11
34
12
:: alert { type =" danger " title =" 特别注意 " }
35
- 因为离线图标是按需引入的,UnoCSS无法通过动态的方式解析除` .jsx ` , ` .tsx ` , ` .vue ` 等以外的如 ` .ts ` , ` .js ` 文件中的图标,所以如果出现无法显示icon图标的时候,可以尝试将图标的名称添加到 ` @/unocss.config.ts ` 的 ` safelist ` 属性中 :
13
+ 因为离线图标是按需引入的,UnoCSS无法通过动态的方式解析除` .jsx ` , ` .tsx ` , ` .vue ` 等以外的如 ` .ts ` , ` .js ` 文件中的图标,所以如果出现无法显示icon图标的时候,可以修改 ` @/unocss.config.ts ` 来实现手动引入 :
36
14
``` ts{}[@/unocss.config.ts]
37
15
export default defineConfig({
38
- ...
16
+ // 方式 1. 添加 unocss 扫描文件类型,开发时需重启
17
+ content: {
18
+ pipeline: {
19
+ include: [
20
+ /\.(ts|vue|html)($|\?)/,
21
+ ]
22
+ }
23
+ },
24
+ // 方式 2. 添加图标名称到 safelist 中
39
25
safelist: [
40
26
'i-twemoji-blue-book',
41
27
'i-twemoji-books',
42
28
'i-twemoji-rocket',
43
29
],
44
- ...
30
+ // 方式 3. 在需要引入的文件中添加扫描标识:@unocss-include
45
31
})
46
32
```
47
33
::
48
- ### 使用 Svg 图标
34
+ ## 使用 Svg 图标
49
35
将下载的 ` .svg ` 图标放在项目 ` @/assets/svg ` 文件夹下,然后就可以直接在代码中引用到了。` name ` 属性为 ` svg-${svgFileName} `
50
36
** 输入**
51
37
``` html
52
- <AdminIcon name =" svg-train" class =" text-2xl" />
38
+ <Icon name =" svg-train" class =" text-2xl" />
53
39
```
54
40
** 输出**
55
- <AdminIcon name =" svg-train " class =" text-2xl " />
41
+ <Icon name =" svg-train " class =" text-2xl " />
56
42
57
43
** 输入**
58
44
``` html
59
- <AdminIcon name =" svg-train" filled class =" text-4xl" />
45
+ <Icon name =" svg-train" filled class =" text-4xl" />
60
46
```
61
47
** 输出**
62
- <AdminIcon name =" svg-train " filled class =" text-4xl " />
48
+ <Icon name =" svg-train " filled class =" text-4xl " />
63
49
64
50
### 使用 iconify 图标
65
- 当 ` <AdminIcon /> ` 图标组件的 ` name ` 属性为 ` i-${iconifyName} ` 的时候,图标将解析为 iconify 图标。
51
+ 当 ` <Icon /> ` 图标组件的 ` name ` 属性为 ` i-${iconifyName} ` 的时候,图标将解析为 iconify 图标。
66
52
** 输入**
67
53
``` html
68
- <AdminIcon name =" i-twemoji-sun" class =" text-2xl" ></AdminIcon >
69
- <AdminIcon name =" i-twemoji-rocket" class =" text-2xl" ></AdminIcon >
70
- <AdminIcon name =" i-twemoji-blue-book" class =" text-2xl" ></AdminIcon >
71
- <AdminIcon name =" i-twemoji-books" class =" text-2xl" ></AdminIcon >
54
+ <Icon name =" i-twemoji-sun" class =" text-2xl" ></Icon >
55
+ <Icon name =" i-twemoji-rocket" class =" text-2xl" ></Icon >
56
+ <Icon name =" i-twemoji-blue-book" class =" text-2xl" ></Icon >
57
+ <Icon name =" i-twemoji-books" class =" text-2xl" ></Icon >
72
58
```
73
59
** 输出**
74
- <AdminIcon name =" i-twemoji-sun " class =" text-2xl " ></AdminIcon >
75
- <AdminIcon name =" i-twemoji-rocket " class =" text-2xl " ></AdminIcon >
76
- <AdminIcon name =" i-twemoji-blue-book " class =" text-2xl " ></AdminIcon >
77
- <AdminIcon name =" i-twemoji-books " class =" text-2xl " ></AdminIcon >
60
+ <Icon name =" i-twemoji-sun " class =" text-2xl " ></Icon >
61
+ <Icon name =" i-twemoji-rocket " class =" text-2xl " ></Icon >
62
+ <Icon name =" i-twemoji-blue-book " class =" text-2xl " ></Icon >
63
+ <Icon name =" i-twemoji-books " class =" text-2xl " ></Icon >
78
64
79
- ## 通过 ` unocss ` 和 ` iconify ` 使用图标
80
- 项目预先安装了以下4套图标:
81
- :: list { type =" success " }
82
- - ` @iconify-json/ep ` ,element-plus 图标库
83
- - ` @iconify-json/carbon ` ,carbon 图标库
84
- - ` @iconify-json/logos ` ,各类 logo 图标
85
- - ` @iconify-json/twemoji ` ,twitter emoji 图标
86
- ::
87
- 使用的时候给` <div> ` 或 ` <i> ` 加上 ` class="i-icon-name" ` 的样式即可:
65
+ ## 通过 ` unocss ` 和 ` iconify ` 来使用图标
66
+ 可以根据 ` unocss ` 的文档,给` <div> ` 或 ` <i> ` 加上 ` class="i-icon-name" ` 的样式即可渲染出图标:
88
67
** 输入**
89
68
``` html
90
69
<div class =" i-twemoji-sun inline-block text-2xl" ></div >
@@ -97,22 +76,3 @@ export default defineConfig({
97
76
<div class =" i-twemoji-rocket inline-block text-2xl " ></div >
98
77
<div class =" i-twemoji-blue-book inline-block text-2xl " ></div >
99
78
<div class =" i-twemoji-books inline-block text-2xl " ></div >
100
-
101
- ## 通过 ` <AdminIconOnline /> ` 使用在线图标
102
-
103
- 在线图标是指在渲染时通过互联网获取的图标,没有打包和引入项目,直接使用图标名字(不用提前引入项目)。
104
- :: alert { type =" tip " }
105
- ** ` iconify ` ** 集成了上千种图标,都可以通过 ` API ` 在线获取,具体实现可以看[ https://docs.iconify.design/icon-components/vue/ ] ( https://docs.iconify.design/icon-components/vue/ )
106
-
107
- ::
108
- 使用方法是在 [ iconify 图标集网站] ( https://icones.js.org/ ) 上找到所需的图标,直接复制到 name 属性即可:
109
- ** 输入**
110
- ``` html
111
- <AdminIconOnline name =" vscode-icons:file-type-azurepipelines" class =" text-4xl" ></AdminIconOnline >
112
- <AdminIconOnline name =" logos:steam" class =" text-4xl" ></AdminIconOnline >
113
- <AdminIconOnline name =" logos:tiktok" class =" text-4xl" ></AdminIconOnline >
114
- ```
115
- ** 输出**
116
- <AdminIconOnline name =" vscode-icons:file-type-azurepipelines " class =" text-4xl " ></AdminIconOnline >
117
- <AdminIconOnline name =" logos:steam " class =" text-4xl " ></AdminIconOnline >
118
- <AdminIconOnline name =" logos:tiktok " class =" text-4xl " ></AdminIconOnline >
0 commit comments