教你在vue項(xiàng)目中使用svg圖標(biāo)的方法
- svg圖標(biāo)優(yōu)點(diǎn)
svg與iconfont之類(lèi)的字體圖標(biāo)在網(wǎng)頁(yè)中的使用差別不大,可以修改大小,顏色等而且不失真。
- 安裝
svg-sprite-loader
npm install --save-dev svg-sprite-loader
- 文件夾目錄 (xxx.svg 注意:這里的 xxx 不要使用中文)
- assets -- icon --- svg --- index.js
- 配置依賴(lài)
// Vue2.x 在 webpack.base.conf.js 中配置如下: // 注意svg圖標(biāo)的路徑 src/assets/icon 要寫(xiě)正確 module: { rules: [ { test: /\.svg$/, loader: 'svg-sprite-loader', include: [resolve('src/assets/icon')], options: { symbolId: 'icon-[name]' } }, { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, loader: 'url-loader', exclude: [resolve('src/assets/icon')], options: { limit: 10000, name: utils.assetsPath('img/[name].[hash:7].[ext]') } } ] }
// Vue3.x 在根目錄新建 vue.config.js 中配置如下: module.exports = { chainWebpack: config => { const svgRule = config.module.rule('svg') svgRule.uses.clear() svgRule.exclude.add(/node_modules/) svgRule .test(/\.svg$/) .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'icon-[name]', }) // 修改images loader 添加svg處理 const imagesRule = config.module.rule('images') imagesRule.exclude.add(resolve('src/assets/icon')) config.module .rule('images') .test(/\.(png|jpe?g|gif|svg)(\?.*)?$/) } }
// Vue4.x 在根目錄新建 vue.config.js 中配置如下: const path = require('path') module.exports = { // 使用svg-sprite-loader編譯svg,若使用file-loader時(shí)排除src/icon下的svg矢量圖標(biāo) chainWebpack: (config) => { const svgRule = config.module.rule('svg') // 清除已有的所有 loader 否則接下來(lái)的 loader 會(huì)附加在該規(guī)則現(xiàn)有的 loader 之后 svgRule.uses.clear() svgRule .test(/\.svg$/) .include.add(path.resolve(__dirname, './src/icon')) .end() .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'icon-[name]', }) const fileRule = config.module.rule('file') fileRule.uses.clear() fileRule .test(/\.svg$/) .exclude.add(path.resolve(__dirname, './src/icon')) .end() .use('file-loader') .loader('file-loader') }, }
- 在components目錄下增加一個(gè)
SvgIcon
組件
<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> // 檢查是否是外部鏈接 var isExternal function(path) { return /^(https?:|mailto:|tel:)/.test(path) } 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>
- 在
icon
文件夾下index.js
中導(dǎo)入所有svg文件,并將SvgIcon注冊(cè)到全局
import Vue from 'vue' import SvgIcon from '@/components/SvgIcon' // 全局注冊(cè) Vue.component('svg-icon', SvgIcon) const req = require.context('./svg', false, /\.svg$/) const requireAll = requireContext => requireContext.keys().map(requireContext) requireAll(req)
- 在
main.js
文件中引入 svg 配置
import '@/assets/icon'
- 使用
<!-- 其中icon-class對(duì)應(yīng)圖標(biāo)svg文件的名稱(chēng);className對(duì)應(yīng)圖標(biāo)的css樣式屬性 --> <svg-icon icon-class="arrow" className="left-arrow"></svg-icon>
- 備注
若svg圖標(biāo)不能通過(guò)樣式修改顏色,打開(kāi)svg文件,刪除style標(biāo)簽里的每一項(xiàng)fill樣式設(shè)置。但是如果svg文件中使用的不是 path 那就沒(méi)有辦法了。比如有些在線(xiàn)的工具可以把圖片轉(zhuǎn)成svg格式,轉(zhuǎn)換后svg文件中的地址是 base64 ,這種的就不能改變樣式了,而且放縮也會(huì)失真。
到此這篇關(guān)于教你在vue項(xiàng)目中使用svg圖標(biāo)的方法的文章就介紹到這了,更多相關(guān)vue使用svg圖標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue.js 遞歸組件實(shí)現(xiàn)樹(shù)形菜單(實(shí)例分享)
本文主要對(duì)介紹利用Vue.js 的遞歸組件,實(shí)現(xiàn)了一個(gè)最基本的樹(shù)形菜單。具有很好的參考價(jià)值,下面就跟著小編一起來(lái)看下吧2016-12-12unplugin-auto-import與unplugin-vue-components安裝問(wèn)題解析
這篇文章主要為大家介紹了unplugin-auto-import與unplugin-vue-components問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02vue.js組件vue-waterfall-easy實(shí)現(xiàn)瀑布流效果
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)瀑布流之vue-waterfall-easy的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08解決vue?vite啟動(dòng)項(xiàng)目報(bào)錯(cuò)ERROR:?Unexpected?“\x88“?in?JSON?的問(wèn)題
這篇文章主要介紹了vue?vite啟動(dòng)項(xiàng)目報(bào)錯(cuò)ERROR:?Unexpected?“\x88“?in?JSON?原因,本文給出出現(xiàn)此類(lèi)問(wèn)題的原因所在并給出解決方法,需要的朋友可以參考下2022-09-09vue+vant實(shí)現(xiàn)購(gòu)物車(chē)全選和反選功能
這篇文章主要為大家詳細(xì)介紹了vue+vant實(shí)現(xiàn)購(gòu)物車(chē)全選和反選功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11Vue中使用定時(shí)器(setInterval、setTimeout)的兩種方式
js中定時(shí)器有兩種,一個(gè)是循環(huán)執(zhí)行?setInterval,另一個(gè)是定時(shí)執(zhí)行?setTimeout,這篇文章主要介紹了Vue中使用定時(shí)器?(setInterval、setTimeout)的兩種方式,需要的朋友可以參考下2023-03-03