美文网首页
svg图标封装

svg图标封装

作者: imTinaz | 来源:发表于2018-12-04 11:32 被阅读0次

PS:注意如果在iconfont上下载的SVG图标,请把fill="#ffffff"清除掉,不然自定义样式无法作用到图标上。

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’

相关文章

网友评论

      本文标题:svg图标封装

      本文链接:https://www.haomeiwen.com/subject/eygmfqtx.html