在vue3中使用icon圖標的三種方案
1. element-icon
Element Plus 提供了一套常用的圖標集合。
1.1. 安裝
# 選擇一個你喜歡的包管理器 # NPM $ npm install @element-plus/icons-vue # Yarn $ yarn add @element-plus/icons-vue # pnpm $ pnpm install @element-plus/icons-vue# 選擇一個你喜歡的包管理器
1.2. 注冊所有圖標
從 @element-plus/icons-vue 中導(dǎo)入所有圖標并進行全局注冊。
// main.ts // 如果您正在使用CDN引入,請刪除下面一行。 import * as ElementPlusIconsVue from '@element-plus/icons-vue' const app = createApp(App) for (const [key, component] of Object.entries(ElementPlusIconsVue)) { app.component(key, component) }
1.3. 基礎(chǔ)用法
<!-- 使用 el-icon 為 SVG 圖標提供屬性 --> <template> <div> <el-icon :size="size" :color="color"> <Edit /> </el-icon> <!-- 或者獨立使用它,不從父級獲取屬性 --> <Edit /> </div> </template>
如果你想用字符串的形式,可以這樣搞。
以側(cè)邊欄的圖標渲染為例子。
我的路由是這樣寫的:
{ path: '/index', name: 'Index', component: () => import('@/views/workbench/home/index.vue'), meta: { title: '工作臺', icon: 'HomeFilled', affix: true, }
當(dāng)在組件中渲染的時候可以用component
組件來渲染:
<el-menu-item :index="subItem.path" @click="handleClickMenu(subItem)" > <el-icon> <component :is="subItem.meta.icon"></component> </el-icon> </el-menu-item>
最終效果:
2. svg-icon
如果element的icon不滿足我們的需求的話,我們可以從iconfont去下載svg圖標。然后使用。
2.1. 安裝
首先要使用vite-plugin-svg-icons插件
yarn add vite-plugin-svg-icons -D # or npm i vite-plugin-svg-icons -D # or pnpm install vite-plugin-svg-icons -D
2.2. 配置
在vite.config.ts中配置一下
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons' import path from 'path' export default () => { return { plugins: [ createSvgIconsPlugin({ iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')], symbolId: 'icon-[dir]-[name]', }), ], } }
這里注意iconDirs的路徑,是讀取的svg icon存放的目錄。
2.3. 封裝
我們把Svg封裝成一個組件:
<template> <i :class="['el-icon', spin && 'svg-icon-spin']" :style="getStyle"> <svg aria-hidden="true"> <use :xlink:href="symbolId" rel="external nofollow" :fill="color" /> </svg> </i> </template> <script setup lang="ts" name="SvgIcon"> import { computed } from 'vue' import type { CSSProperties } from 'vue' const props = defineProps({ prefix: { type: String, default: 'icon', }, name: { type: String, required: true, }, color: { type: String, default: '#ffffff', }, size: { type: [Number, String], default: 20, }, spin: { type: Boolean, default: false, }, }) const symbolId = computed(() => `#${props.prefix}-${props.name}`) const getStyle = computed((): CSSProperties => { const { size } = props let s = `${size}` s = `${s.replace('px', '')}px` return { fontSize: s, } }) </script> <style scoped lang="scss"> .el-icon { --color: inherit; position: relative; display: inline-flex; align-items: center; justify-content: center; width: 1em; height: 1em; font-size: inherit; line-height: 1em; color: var(--color); fill: currentColor; svg { width: 1em; height: 1em; } } .svg-icon-spin { animation: circle 1.5s infinite linear; } /* 旋轉(zhuǎn)動畫 */ @keyframes circle { 0% { transform: rotate(0); } 100% { transform: rotate(360deg); } } </style>
這里我封裝的支持prefix、name、size、color、spin(是否旋轉(zhuǎn))等屬性。
2.4. 使用
我們先去iconfont下載一個svg格式的圖標。
下載完成后,把這個svg放入iconDirs聲明的路徑下面即可:
在項目中使用。引入這個組件然后使用即可。注意name跟你的svg name保持一致。
<SvgIcon name="welcome" size="400px" /><SvgIcon name="welcome" size="400px" />
我這里的圖標效果是這樣的:
3. iconify
iconify是一種非常廣泛的圖標解決方案,它收集了全網(wǎng)所有的圖標。
3.1. 安裝
pnpm install --save-dev @iconify/vuepnpm install --save-dev @iconify/vue
3.2. 封裝
import { h, defineComponent } from 'vue' import { Icon as IconifyIcon } from '@iconify/vue' export default defineComponent({ name: 'IconifyIconOnline', components: { IconifyIcon }, props: { icon: { type: String, default: '', }, }, render() { const attrs = this.$attrs return h( IconifyIcon, { icon: `${this.icon}`, style: attrs?.style ? Object.assign(attrs.style, { outline: 'none' }) : { outline: 'none' }, ...attrs, }, { default: () => [], }, ) }, })
當(dāng)然你不封裝也可以。
3.3. 使用
首先我們要找一個圖標,可以去icon-sets.iconify.design/。搜索你想要的圖標。
復(fù)制圖標的名字。
在項目中直接使用
<template> <div class="btn"> <el-tooltip content="刷新"> <el-button circle> <IconifyIcon icon="ri:refresh-line" height="16" /> </el-button> </el-tooltip> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' import { IconifyIcon } from '@/components/IconifyIcon' export default defineComponent({ components: { IconifyIcon, }, }) </script> <style scoped lang="scss"> .btn { margin-right: 20px; cursor: pointer; transition: all 0.3s; }
如果你想直接在vscode中預(yù)覽這個圖標長啥樣,就像下面這樣:
你可以安裝一個插件:Iconify IntelliSense
我們在瀏覽器中打開調(diào)試工具,看看application,發(fā)現(xiàn)這里緩存的一些圖標
當(dāng)?shù)谝淮握埱蠛?,瀏覽器會把這個圖標緩存。下次請求的時候直接從緩存中讀取的。
最后
以上,就是我在項目中使用圖表的三種方式。你還有其他的方式嗎?歡迎在評論區(qū)討論。
界面展示:
以上就是在vue3中使用icon圖標的三種方案的詳細內(nèi)容,更多關(guān)于vue3使用icon圖標的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue中監(jiān)聽input框獲取焦點及失去焦點的問題
這篇文章主要介紹了vue中監(jiān)聽input框獲取焦點,失去焦點的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07一文詳解vue各種權(quán)限控制與管理實現(xiàn)思路
這篇文章主要為大家介紹了vue各種權(quán)限控制與管理的實現(xiàn)思路詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03vue-router之nuxt動態(tài)路由設(shè)置的兩種方法小結(jié)
今天小編就為大家分享一篇vue-router之nuxt動態(tài)路由設(shè)置的兩種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09Vue3?封裝一個支持輸入和單/多選InputSelect組件-Antd詳解
Antd的Select組件默認不支持作為輸入框使用或手動添加選項,為了實現(xiàn)這一功能,我們封裝了一個通用組件,支持單選和多選模式,并允許用戶在組件失焦時手動輸入選項,主要通過定義searchText存儲輸入數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧2024-09-09vue項目發(fā)布有緩存正式環(huán)境不更新的解決方案
vue項目每次發(fā)布新版本后,測試人員都要強制刷新才能更新瀏覽器代碼來驗證bug,下面這篇文章主要給大家介紹了關(guān)于vue項目發(fā)布有緩存正式環(huán)境不更新的解決方案,需要的朋友可以參考下2024-03-03vue使用$emit時,父組件無法監(jiān)聽到子組件的事件實例
下面小編就為大家分享一篇vue使用$emit時,父組件無法監(jiān)聽到子組件的事件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02vue 調(diào)用 RESTful風(fēng)格接口操作
這篇文章主要介紹了vue 調(diào)用 RESTful風(fēng)格接口操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08