本文参考了解决element-plus使用图标不显示问题,并做了一些修订删改。
前提:确定安装了element-plus/icons
npm install @element-plus/icons-vue
如果图标引用的比较少,那么采用方案1,如果大量的使用了图标,就采用方案2
方案1:
在引用图标的页面中将要使用的图标引入,作为js对象,代码如下:
<div class="flex">
<el-button type="primary" :icon="Edit" />
<el-button type="primary" :icon="Share" />
<el-button type="primary" :icon="Delete" />
<el-button type="primary" :icon="Search">Search</el-button>
<el-button type="primary">
Upload<el-icon class="el-icon--right"><Upload /></el-icon>
</el-button>
</div>
<script>
import { Edit,Share } from "@element-plus/icons-vue";
export default {
setup() {
return {
Edit,
Share
}
}
}
</script>
这就是elemunt-ui3官网一致示例中的使用方式
方案2:
方案2其实是针对方案一的一个优化,对于大量使用icon的项目来说相对比较友好
首先:在main.js中将icon全部引入并注册到页面中
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElIconList from '@element-plus/icons-vue'
for (const name in ElIconList) {
app.component(name, ElIconList[name])
}
前面两个import是导入ElementPlus和ElementPlus的CSS样式
然后是导入并一次性添加所有的ElementPlus ICON
然后就是使用了,在页面中直接使用即可
<el-button-group class="ml-4">
<el-button type="primary" icon="Upload" />
<el-button type="primary" icon="Edit" />
<el-button type="primary" icon="Share" />
<el-button type="primary" icon="Delete" />
</el-button-group>
可以看到,使用方案2最大的区别就是icon属性是直接赋值,而不是v-bind绑定(:icon)。