Vue中引入svg圖標(biāo)的兩種方式
Vue中引入svg圖標(biāo)的方式
Vue中引入svg圖標(biāo)的方式一
安裝
yarn add svg-sprite-loader --dev
svg組件

index.vue
<!-- svg組件 -->
<template>
<svg class="svg-icon" :class="svgClass" aria-hidden="true">
<use :xlink:href="iconName" />
</svg>
</template>
<script>
export default {
name: 'SvgIcon',
props: {
// svg 的名稱
svgName: {
type: String,
required: true
}
},
computed: {
iconName () {
return `#icon-${this.svgName}`
},
svgClass () {
if (this.svgName) {
return 'svg-icon' + this.svgName
} else {
return 'svg-icon'
}
}
}
}
</script>
<style lang="less" scoped>
.svg-icon {
width: 100px;
height: 100px;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}
</style>
注冊(cè)到全局

index.js
import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'
// 注冊(cè)到全局
Vue.component('svg-icon', SvgIcon)
const requireAll = requireContext => requireContext.keys().map(requireContext)
const req = require.context('./svg', false, /\.svg$/)
requireAll(req)
vue.config.js
module.exports = {
chainWebpack: config => {
config.module
.rule('svg')
.exclude.add(resolve('src/assets/icons'))
.end()
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/assets/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end()
}
}
頁(yè)面中使用
<!-- svg-name為svg名 --> <svg-icon svg-name="ic_home_news" />
Vue中引入svg圖標(biāo)的方式二
npm install svg-sprite-loader --save-dev
vue.config.js中添加如下代碼
const path = require('path');
function resolve(dir) {
// __dirname項(xiàng)目根目錄的絕對(duì)路徑
return path.join(__dirname, dir);
}
module.exports = {
chainWebpack: config => {
const svgRule = config.module.rule('svg');
// 清除已有的所有l(wèi)oader
// 如果你不這樣做,接下來(lái)的loader會(huì)附加在該規(guī)則現(xiàn)有的loader之后
svgRule.uses.clear();
svgRule
.test(/\.svg$/)
.include.add(path.resolve(__dirname, './src/icons/svg'))
.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/icons/svg'))
.end()
.use('file-loader')
.loader('file-loader');
},
}
建立如下的文件目錄

SvgIcon.vue代碼
<template>
<svg :class="svgClass" xmlns="http://www.w3.org/2000/svg">
<use :xlink:href="iconName" xmlns:xlink="http://www.w3.org/1999/xlink" />
</svg>
</template>
<script>
export default {
name: 'SvgIcon',
props: {
iconClass: {
type: String,
required: true
},
className: {
type: String,
default: ''
}
},
computed: {
iconName() {
return `#icon-${this.iconClass}`;
},
svgClass() {
if (this.className) {
return 'svg-icon ' + this.className;
} else {
return 'svg-icon';
}
}
}
};
</script>
<style scoped>
.svg-icon {
width: 1em;
height: 1em;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}
</style>
svg文件夾下放svg圖標(biāo)
index.js代碼
import Vue from 'vue';
import SvgIcon from '@/components/SvgIcon'; // svg組件
// register globally
Vue.component('svg-icon', SvgIcon);
const req = require.context('./svg', false, /\.svg$/);
const requireAll = requireContext => requireContext.keys().map(requireContext);
requireAll(req);
最后在main.js中引入
import './icons';
在頁(yè)面中使用svg
icon-class是svg圖標(biāo)名 class-name是你要自定義的class類名
<svg-icon icon-class="features_ic_risk@1x" class-name="icon"></svg-icon>
總結(jié)
到此這篇關(guān)于Vue中引入svg圖標(biāo)的兩種方式的文章就介紹到這了,更多相關(guān)Vue引入svg圖標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決VantUI popup 彈窗不彈出或無(wú)蒙層的問(wèn)題
這篇文章主要介紹了解決VantUI popup 彈窗不彈出或無(wú)蒙層的問(wèn)題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
使用vue初用antd 用v-model來(lái)雙向綁定Form表單問(wèn)題
這篇文章主要介紹了使用vue初用antd 用v-model來(lái)雙向綁定Form表單問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
vuex 實(shí)現(xiàn)getter值賦值給vue組件里的data示例
今天小編就為大家分享一篇vuex 實(shí)現(xiàn)getter值賦值給vue組件里的data示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Vue利用自定義指令實(shí)現(xiàn)按鈕權(quán)限控制
這篇文章主要為大家詳細(xì)介紹了Vue如何利用自定義指令實(shí)現(xiàn)按鈕權(quán)限控制效果,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考下2023-05-05
Vue的事件響應(yīng)式進(jìn)度條組件實(shí)例詳解
這篇文章主要介紹了Vue的事件響應(yīng)式進(jìn)度條組件的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02
vue如何使用formData傳遞文件類型的數(shù)據(jù)
這篇文章主要介紹了vue如何使用formData傳遞文件類型的數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
誤引用vuex-persistedstate導(dǎo)致用戶信息無(wú)法清除問(wèn)題及解決
這篇文章主要介紹了誤引用vuex-persistedstate導(dǎo)致用戶信息無(wú)法清除問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
VUE 實(shí)現(xiàn)一個(gè)簡(jiǎn)易老虎機(jī)的項(xiàng)目實(shí)踐
老虎機(jī)在很多地方都可以見(jiàn)到,可以設(shè)置中獎(jiǎng)位置,以及中獎(jiǎng)回調(diào),本文主要介紹了VUE 實(shí)現(xiàn)一個(gè)簡(jiǎn)易老虎機(jī)的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

