這篇文章主要介紹“Element Plus的el-icon如何用”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Element Plus的el-icon如何用”文章能幫助大家解決問題。
在 Vue 生態里, Element UI 是排名前列的組件庫。 在 Vue 發布到 3.0 時,Element 也發布了對應的組件庫。也就是 Element Plus 。隨之而來的用法也跟著變了。
比如本文要講的 el-icon 的用法。
在 Element Plus 里,Icon 圖標 的用法和以前不一樣了。雖然官方文檔也有說明怎么用,但不是非常詳細,可能會給新手帶來一丟丟障礙。
本文將花幾分鐘的時間講解 el-icon 幾種用法和注意事項。
注意:需要留意本文發表時間與使用的 Element Plus 版本,隨著時間的推移可能會出現使用上的差異。
vue: ^3.2.25
element-plus: ^2.1.7
@element-plus/icons-vue: ^1.1.4
在 vue2 + Element UI 的用法
<i class="el-icon-edit"></i>
在 vue3 + Element Plus 的用法
<ElIcon :size="30" color="hotpink"> <edit /> </ElIcon> <!-- 也可以直接使用圖標標簽,無需父標簽包裹 --> <edit />
個人覺得,Element UI 的用法會更加簡單。
下一篇文章我會講解如何在 Element Plus 的基礎上二次封裝出一個更好用的 Icon組件 。
Element Plus 拋棄了字體圖標的用法,直接使用了 svg 的方式。
可以說,圖標這個東西被拎出來單獨維護了。所以在使用前必須把 svg圖標庫 下載下來。
下載 svg圖標庫 的命令:
npm install @element-plus/icons-vue
你也可以使用 Yarn 或 pnpm 的方式下載
# Yarn yarn add @element-plus/icons-vue # pnpm pnpm install @element-plus/icons-vue
使用的方式有2種,一種是直接使用 svg,另一種是配合 el-icon 標簽一起使用。
接下來就分別講講這兩種使用方式(全局和局部引入都會講到)
如果你只需使用 Element Plus 提供的 svg圖標庫 的話,是可以不安裝 Element Plus 的。不過這種場景應該很少出現。
安裝命令:
npm install @element-plus/icons-vue
Element Plus 提供的 svg圖標 種類可以到 圖標集合 里查看。
通過 svg組件 的方式使用圖標,如需設置圖標大小和顏色,都需要通過 css 來設置。
全部引入的方式會將所有 svg組件 都注冊到全局,用的時候比較方便,但會犧牲一點性能。
main.js
import { createApp } from 'vue'
import App from './App.vue'
import * as Icons from '@element-plus/icons-vue' // 引入所有圖標,并命名為 Icons
const app = createApp(App)
// 通過遍歷的方式注冊所有 svg組件,會犧牲一點點性能
for (let i in Icons) {
app.component(i, Icons[i])
}
app.mount('#app')如果你不想全部引入,只是想在全局注冊某個 svg圖標組件,可以用以下方式在 main.js 里注冊(我以 Edit 圖標為例)
/* 省略部分代碼 */
import { Edit } from '@element-plus/icons-vue' // 引入 Edit 圖標
const app = createApp(App)
app.component(Edit.name, Edit) // 全局注冊 Edit 圖標
app.mount('#app')在頁面中使用
<template>
<div>
<edit />
</div>
</template>
<style>
svg {
width: 40px;
height: 40px;
color: red;
}
</style>局部引入的方式只需在使用的地方引入即可。
<template>
<div>
<edit />
</div>
</template>
<script setup>
import { Edit } from '@element-plus/icons-vue' // 引入 Edit 這個 svg組件
</script>
<style>
svg {
width: 40px;
height: 40px;
color: red;
}
</style>Element Plus 還提供了 el-icon 組件用來包裹 svg圖標組件 ,使得設置圖標大小和顏色更加方便。
但需要在項目中安裝 Element Plus ,安裝命令如下:
# 選擇其中一種方式安裝即可。 # NPM npm install element-plus --save # Yarn yarn add element-plus # pnpm pnpm install element-plus
安裝完 Element Plus 后,可以在全局引入,也可以局部引入。
main.js
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import { Edit } from '@element-plus/icons-vue' // 引入 Edit 圖標
import App from './App.vue'
const app = createApp(App)
app.component(Edit.name, Edit) // 全局注冊 Edit 圖標
app
.use(ElementPlus)
.mount('#app')在頁面中使用
<el-icon :size="20" color="hotpink"> <edit /> </el-icon>
此時,在 el-icon 上設置 size 和 color 就能控制 svg圖標 的大小和顏色。
需要注意的是 size 屬性必須傳數字,不能傳字符串進去!
<template>
<div>
<el-icon :size="30" color="hotpink">
<edit />
</el-icon>
</div>
</template>
<script setup>
import { ElIcon } from 'element-plus'
import { Edit } from '@element-plus/icons-vue'
import 'element-plus/es/components/icon/style/css'
</script>局部引入的話,我們只需要引入 icon 對應的 css 即可。
如果你在 main.js 引入了 element-plus/dist/index.css 就不需要在頁面再引入 element-plus/es/components/icon/style/css 。
關于“Element Plus的el-icon如何用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。