Vue封裝svg-icon組件使用教程
一、SVG可縮放矢量圖形
SVG(Scalable Vector Graphics)可縮放矢量圖形,是一種用于描述基于二維的矢量圖形的 XML 標(biāo)記語言,其基本矢量顯示對(duì)象包括矩形、圓、橢圓、多邊形、直線、任意曲線等,還能顯示文字對(duì)象和嵌入式外部圖像(包括 PNG、JPEG、SVG 等)。實(shí)際項(xiàng)目中大多數(shù)圖標(biāo)都是使用的 SVG 圖標(biāo)文件,其主要有以下幾個(gè)優(yōu)點(diǎn):
1.內(nèi)容可讀,文件是純粹的 XML。
2.圖像文件小,可伸縮性強(qiáng)。
3.矢量放縮,能以不犧牲圖像質(zhì)量為前提,進(jìn)行任意縮放。
4.還能基于 DOM 模型實(shí)現(xiàn)動(dòng)態(tài)和一些交互功能
二、SVG在vue項(xiàng)目中的配置與使用
1. 下載
npm install vue-svg-icon xml-loader -D
2. 下載的.svg的文件,存放于src/icons/svg文件
3. 配置src/icons/index.js文件
import Vue from 'vue' import SvgIcon from '@/components/SvgIcon'// svg component // register globally Vue.component('svg-icon', SvgIcon) // 在./svg下查找 “.svg”文件 const req = require.context('./svg', false, /\.svg$/) const requireAll = requireContext => requireContext.keys().map(requireContext) requireAll(req)
4.在main.js中引入icon/index.js文件中全局設(shè)置
//全局加載就可以了 import '@/icons/index.js'
5. 封裝組件
/src/components/svgIcon/index.vue
<template> <div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$listeners" /> <svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners"> <use :xlink:href="iconName" rel="external nofollow" /> </svg> </template> <script> // doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usage import { isExternal } from '@/utils' export default { name: 'SvgIcon', props: { iconClass: { type: String, required: true }, className: { type: String, default: '' } }, computed: { isExternal() { return isExternal(this.iconClass) }, iconName() { return `#icon-${this.iconClass}` }, svgClass() { if (this.className) { return 'svg-icon ' + this.className } else { return 'svg-icon' } }, styleExternalIcon() { return { mask: `url(${this.iconClass}) no-repeat 50% 50%`, '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%` } } } } </script> <style 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>
6.頁面使用
<div> <svg-icon icon-class="peoples" /> </div>
到目前為止使用 < svg-icon icon-class=“left-indent” /> 還沒有效果,因?yàn)檫€缺少配置文件
7. vue.config.js 中配置 svg-sprite-loader
安裝:
npm install svg-sprite-loader -D
配置 vue.config.js:
使用chainWebpack方法可自定義配置 loader vue.config.js
module.exports = { chainWebpack: config => { 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() } }
重啟項(xiàng)目遇到報(bào)錯(cuò):error: resolve is not defined
在vue.config.js中沒有配置 resolve 方法, 需要自定義一個(gè),代碼如下
const path = require('path') function resolve(dir) { return path.join(__dirname, dir) }
此時(shí),圖標(biāo)在頁面已經(jīng)出效果;
到此這篇關(guān)于Vue封裝svg-icon組件使用教程的文章就介紹到這了,更多相關(guān)Vue svg icon組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue為何棄用Ajax,選擇Axios?ajax與axios的區(qū)別?
ajax技術(shù)實(shí)現(xiàn)了局部數(shù)據(jù)的刷新,axios實(shí)現(xiàn)了對(duì)ajax的封裝;axios有的ajax都有,ajax有的axios不一定有。總結(jié)一句話就是axios是ajax,ajax不止axios。2023-01-01vue的v-if里實(shí)現(xiàn)調(diào)用函數(shù)
這篇文章主要介紹了vue的v-if里實(shí)現(xiàn)調(diào)用函數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Antd的Table組件嵌套Table以及選擇框聯(lián)動(dòng)操作
這篇文章主要介紹了Antd的Table組件嵌套Table以及選擇框聯(lián)動(dòng)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10Vue.js使用v-show和v-if的注意事項(xiàng)
這篇文章一開始先對(duì)Vue.js中v-show和v-if兩者的區(qū)別進(jìn)行了簡(jiǎn)單的介紹,而后通過圖文詳細(xì)給大家介紹了Vue.js使用v-show和v-if注意的事項(xiàng),有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12vue-virtual-scroll-list虛擬組件實(shí)現(xiàn)思路詳解
這篇文章主要給大家介紹了關(guān)于vue-virtual-scroll-list虛擬組件實(shí)現(xiàn)思路的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02詳解Webstorm 新建.vue文件支持高亮vue語法和es6語法
這篇文章主要介紹了Webstorm 添加新建.vue文件功能并支持高亮vue語法和es6語法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10vue 數(shù)據(jù)(data)賦值問題的解決方案
這篇文章主要介紹了vue 數(shù)據(jù)(data)賦值問題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03