图标
Element Plus 提供了一套常用的图标集合。
图标使用
如果你想像例子一样**直接使用**,你需要全局注册这些组件。
如果你想查看所有可用的 SVG 图标,请查看 @element-plus/[email protected]@element-plus/icons-vue@latest 和源码 element-plus-icons,或者查看 图标集合
安装
使用包管理器
选择一个你喜欢的包管理器。
shell
$ npm install @element-plus/icons-vue
shell
$ yarn add @element-plus/icons-vue
shell
$ pnpm install @element-plus/icons-vue
注册所有图标
您需要从 @element-plus/icons-vue
导入所有图标并进行全局注册。
ts
// main.ts
// if you're using CDN, please remove this line.
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component)
}
你也可以参考这个模板。
浏览器直接引入
直接通过浏览器 HTML 标签导入 Element Plus 图标,并使用全局变量 ElementPlusIconsVue
。
根据不同的 CDN 提供商有不同的引入方式, 我们在这里以 unpkg 和 jsDelivr 举例。 你也可以使用其它的 CDN 提供商。
unpkg
html
<script src="//unpkg.com/@element-plus/icons-vue"></script>
jsDelivr
html
<script src="//cdn.jsdelivr.net.cn/npm/@element-plus/icons-vue"></script>
提示
我们建议使用 CDN 引入 Element Plus 的用户在链接地址上锁定版本,以免将来 Element Plus 升级时受到非兼容性更新的影响。 锁定版本的方法请查看 unpkg.com。
自动导入
使用 unplugin-icons 和 unplugin-auto-import 自动从 iconify 中导入任何图标集合。 你可以参考这个模板。
简单用法
警告
因为 HTML 标准定义了一个名为 menu 的标签,所以为了渲染图标,你需要使用一个别名,如果你直接注册 Menu
,它将不会工作。
vue
<!-- Use el-icon to provide attributes to SVG icon -->
<template>
<div>
<el-icon :size="size" :color="color">
<Edit />
</el-icon>
<!-- Or use it independently without derive attributes from parent -->
<Edit />
</div>
</template>
结合 el-icon
el-icon
为原生 SVG 图标提供了额外的属性,更多细节请阅读到最后。
vue
<template>
<p>
with extra class <b>is-loading</b>, your icon is able to rotate 360 deg in 2
seconds, you can also override this
</p>
<el-icon :size="20">
<Edit />
</el-icon>
<el-icon color="#409efc" class="no-inherit">
<Share />
</el-icon>
<el-icon>
<Delete />
</el-icon>
<el-icon class="is-loading">
<Loading />
</el-icon>
<el-button type="primary">
<el-icon style="vertical-align: middle">
<Search />
</el-icon>
<span style="vertical-align: middle"> Search </span>
</el-button>
</template>
带有额外的类 is-loading,你的图标能够在 2 秒内旋转 360 度,你也可以覆盖它。
直接使用 SVG 图标
vue
<template>
<div style="font-size: 20px">
<!-- Since svg icons do not carry any attributes by default -->
<!-- You need to provide attributes directly -->
<Edit style="width: 1em; height: 1em; margin-right: 8px" />
<Share style="width: 1em; height: 1em; margin-right: 8px" />
<Delete style="width: 1em; height: 1em; margin-right: 8px" />
<Search style="width: 1em; height: 1em; margin-right: 8px" />
</div>
</template>
图标集合
提示
只要你安装了它,你就可以在任何版本中使用 SVG 图标
您可以点击图标来复制它
复制代码
API
属性
名称 | 描述 | 类型 | 默认值 |
---|---|---|---|
color | SVG 标签的 fill 属性 | string | 继承自 color |
size | SVG 图标大小,尺寸 x 尺寸 | number / string | 继承自 font size |
插槽
名称 | 描述 |
---|---|
default | 自定义默认内容 |