PS:注意如果在iconfont上下載的SVG圖標(biāo),請把fill="#ffffff"清除掉陷舅,不然自定義樣式無法作用到圖標(biāo)上环揽。
PS:webpack3及以下和webpack4配置略有不同,?webpack4的SVG配置請參考 潘
npm install -g svgo
svgo -f ./src/icons/svg
npm install svg-spribe-loader -D
配置build/webpack.base.conf.js
{
test: /\.svg$/,
loader: 'svg-sprite-loader',
include: [resolve('src/icons')],
options: {
symbolId: 'icon-[name]'
}
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
exclude: [resolve('src/icons')],
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
組件:svgicon.vue
、间护、、
<template>
<svg :class="svgClass" aria-hidden="true">
<use :xlink:href="iconName"/>
</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>
挖诸、汁尺、、
src/icons/index.js
多律、痴突、、
import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon' // svg組件
// register globally
Vue.component('svg-icon', SvgIcon)
const requireAll = requireContext => requireContext.keys().map(requireContext)
const req = require.context('./svg', false, /\.svg$/)
requireAll(req)
狼荞、辽装、、
main.js
import ‘@/icons’