vue3使用element-plus及其icon

本文介绍了如何在Vue项目中安装并配置Element Plus库,包括依赖安装、全局注册图标组件,以及实战示例。重点讲解了如何正确引入和使用带有图标的el-button组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

element-plus

1、安装依赖包

npm install element-plus --save

2、在main.js中引入并注册

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css' 

createApp(App).use(store).use(router).use(ElementPlus).mount('#app')

注意:样式文件也要单独引入

3、使用
在.vue文件中使用element-plusel-button组件试试

    <el-button type="primary">hello element-plus</el-button>

在这里插入图片描述
按钮成功显示!!
再来整一个有图标的按钮
在这里插入图片描述

    <el-button type="primary">hello element-plus</el-button>
    <el-button type="primary">
      <el-icon style="vertical-align: middle">
        <search />
      </el-icon>
      <span style="vertical-align: middle"> Search </span>
    </el-button>

在这里插入图片描述
我的放大镜图标呢?
在这里插入图片描述
查看官方文档后发现,element-plus的icon需要单独下载及引入,具体方法如下

icon部分

1、安装icon的依赖包

npm install @element-plus/icons

2、main.js中全局注册
官方写法

import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

我之前是链式调用注册其它组件的,而此处icon需要循环注册多个,不适合链式调用
所以我换成了和官方相同的全局注册方式

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// createApp(App).use(store).use(router).use(ElementPlus).mount('#app')
const app = createApp(App)
app.use(store)
app.use(router)
app.use(ElementPlus)
app.mount('#app')
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

3、使用
现在我们的放大镜图标就出来了~~
在这里插入图片描述
end.

### 如何在 Vue3Element Plus使用 Icon 图标组件及自定义图标 #### 自定义图标于 `el-rate` 组件中的应用 对于希望在 `el-rate` 组件中引入自定义图标的场景,可以利用该组件的 `icon-components` 属性来指定用于表示星星状态的 SVG 或者其他形式的图标组件。这允许开发者不仅仅局限于官方提供的图标集。 当创建一个自定义图标时,应当将其封装成一个可重用的 Vue 组件[^1]: ```html <template> <!-- 定义评分组件 --> <div> <el-rate v-model="value" :icon-components="customIcons"></el-rate> </div> </template> <script setup> import { ref } from &#39;vue&#39;; // 假设这是两个自定义图标组件 import CustomStarIcon from &#39;./components/CustomStarIcon.vue&#39;; // 自定义未选中星形图标 import CustomActiveStarIcon from &#39;./components/CustomActiveStarIcon.vue&#39;; // 自定义已选中星形图标 const value = ref(0); const customIcons = [ h(CustomStarIcon), h(CustomActiveStarIcon) ]; </script> ``` 上述代码展示了如何通过组合自定义图标与 `el-rate` 的方式实现个性化显示效果。这里的关键在于理解并正确配置 `icon-components` 参数,它接受的是由 VNode 构建函数组成的数组,分别对应着不同状态下要渲染的内容。 #### 动态绑定图标名称 除了静态设置外,在某些情况下可能还需要根据逻辑条件动态改变所使用的图标样式。此时可以通过 `<component>` 标签配合`:is`属性完成这一目标[^2]: ```html <template> <component class="xxx" :is="dynamicIconName"></component> </template> <script setup> import { computed, defineComponent } from "vue"; export default defineComponent({ name: "DynamicIconExample", props: { type: String, }, setup(props) { const dynamicIconName = computed(() => `${props.type}-icon`); return { dynamicIconName, }; } }); </script> ``` 此片段提供了一种方法论上的指导——即依据传入参数决定最终呈现的具体图标实例。这种方式不仅限于简单的字符串替换,还可以进一步扩展到更复杂的业务逻辑判断之中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值