SVG图标使用-vue
ZGuangJu 6/6/2021 svg
# 在vue项目中使用.svg图标,使用iconfont图标库
# 封装svg
- 创建
SvgIcon
组件
在SvgIcon
下创建index.vue
文件
// index.vue
<template>
<svg :class="svgClass" aria-hidden="true">
<use :xlink:href="iconName"></use>
</svg>
</template>
<script>
export default {
name: 'svg-icon',
props: {
iconClass: {
type: String,
required: true
},
className: {
type: String
}
},
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
文件夹,里面创建svg
文件夹和index.js
文件svg
文件夹中用来存放各种扩展的.svg
图标。index.js
种写入以下代码:
// 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'
- 下载插件
npm i svg-sprite-loader --save
- 配置 在build/webpack.base.conf.js文件中,加入
{
test: /\.svg$/,
loader: 'svg-sprite-loader',
include: [resolve('src/icons')],
options: {
symbolId: 'icon-[name]'
}
}
并在以下设置中添加exclude: [resolve(‘src/icons’)],如下所示
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
exclude: [resolve('src/icons')],
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
* 注意里面的icons文件夹的路径,路径错误,图标会显示不出来的。 6. 在项目中使用
<svg-icon icon-class="user" />