Vue.js通過組件處理Icon圖標
?Icon圖標處理方案
記錄一次對于想使用element-plus之外的圖標,如何封裝成一個組件,是本次記錄的目標,希望在工作時能幫助自己處理圖標問題。
分析,對于element-plus的圖標可以通過el-icon來進行顯示,而自定義圖標需要自定義一個圖標組件,用來顯示自定義svg圖標。
組件能力
- 顯示外部svg圖標(外鏈)
- 顯示項目內(nèi)的svg圖標
圖標組件封裝思路
用于展示的圖標組件
創(chuàng)建 components/SvgIcon/index.vue:
<template>
<div
v-if="isExternal"
:style="styleExternalIcon"
class="svg-external-icon svg-icon"
:class="className"
/>
<svg v-else class="svg-icon" :class="className" aria-hidden="true">
<use :xlink:href="iconName" rel="external nofollow" />
</svg>
</template>
?
<script setup>
import { isExternal as external } from '@/utils/validate'
import { defineProps, computed } from 'vue'
const props = defineProps({
// icon圖標
icon: {
type: String,
required: true
},
// 圖標類名
className: {
type: String,
default: ''
}
})
/**
* 判斷是否為外部圖標
*/
const isExternal = computed(() => external(props.icon))
/**
* 外部圖標樣式
*/
const styleExternalIcon = computed(() => ({
mask: `url(${props.icon}) no-repeat 50% 50%`,
'-webkit-mask': `url(${props.icon}) no-repeat 50% 50%`
}))
/**
* 項目內(nèi)圖標
*/
const iconName = computed(() => `#icon-${props.icon}`)
</script>
?
<style lang='scss' scoped>
.svg-icon {
width: 1em;
height: 1em;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}
?
.svg-external-icon {
background-color: currentColor;
mask-size: cover !important;
display: inline-block;
}
</style>
?
判斷資源是否是外部資源
創(chuàng)建 utils/validate.js:
/**
* 判斷是否為外部資源
*/
export function isExternal(path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
外部svg圖標顯示
通過引入組件svg-icon,icon傳遞圖標外鏈。
<span class="svg-container">
<svg-icon icon="https://xxx.svg"></svg-icon>
</span>
處理項目內(nèi)svg圖標
- 項目內(nèi)src目錄下創(chuàng)建 icons 文件夾,并導(dǎo)入svg圖標文件
- 在 icons 下創(chuàng)建 index.js 文件
文件完成兩件事
- 導(dǎo)入所有的svg圖標
- 完成SvgIcon的全局注冊
index.js 代碼如下
import SvgIcon from '@/components/SvgIcon'
?
// 通過 require.context() 函數(shù)來創(chuàng)建自己的 context
const svgRequire = require.context('./svg', false, /\.svg$/)
// 此時返回一個 require 的函數(shù),可以接受一個 request 的參數(shù),用于 require 的導(dǎo)入。
// 該函數(shù)提供了三個屬性,可以通過 require.keys() 獲取到所有的 svg 圖標
// 遍歷圖標,把圖標作為 request 傳入到 require 導(dǎo)入函數(shù)中,完成本地 svg 圖標的導(dǎo)入
svgRequire.keys().forEach(svgIcon => svgRequire(svgIcon))
?
export default app => {
app.component('svg-icon', SvgIcon)
}
全局注冊項目內(nèi)圖標
main.js 中引入該文件
... // 導(dǎo)入 svgIcon import installIcons from '@/icons' ... installIcons(app) ...
使用內(nèi)部圖標
// 用戶名 <svg-icon icon="user" /> // 密碼 <svg-icon icon="password" />
使用 svg-sprite-loader 處理 svg 圖標
svg-sprite-loader是webpack中專門處理svg圖標的一個loader
安裝: npm i --save-dev svg-sprite-loader@6.0.9
在vue.config.js 文件中配置loader
const path = require('path')
function resolve(dir) {
return path.join(__dirname, dir)
}
?
module.exports = {
chainWebpack(config) {
// 設(shè)置 svg-sprite-loader
config.module
.rule('svg')
.exclude.add(resolve('src/icons'))
.end()
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end()
}
}
這樣即可處理內(nèi)部的svg圖標啦。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解使用vue-router進行頁面切換時滾動條位置與滾動監(jiān)聽事件
本篇文章主要介紹了詳解使用vue-router進行頁面切換時滾動條位置與滾動監(jiān)聽事件,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
Vue中實現(xiàn)v-for循環(huán)遍歷圖片的方法
這篇文章主要介紹了Vue中實現(xiàn)v-for循環(huán)遍歷圖片的方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
解決vue-cli項目開發(fā)運行時內(nèi)存暴漲卡死電腦問題
最近開發(fā)一個vue項目時遇到電腦卡死問題,突然間系統(tǒng)就非??ǎ缓罂ㄖㄖ退罊C了,鼠標也動不了了,只能冷啟動。這篇文章主要介紹了vue-cli項目開發(fā)運行時內(nèi)存暴漲卡死電腦問題,需要的朋友可以參考下2019-10-10

