VUE-SVG
2023-02-13 本文已影响0人
O蚂蚁O
1、vue.config.js
chainWebpack: (config) => {
config.module.rules.delete("svg");
config.module
.rule("svg")
.test(/\.svg$/)
.use("svg-sprite-loader")
.loader("svg-sprite-loader")
.options({
symbolId: "icon-[name]",
include: ["src/assets/svg"],
})
.end();
// ============注入cdn start============
config.plugin("html").tap((args) => {
args[0].cdn = cdn;
args[0].title = pageTitle;
return args;
});
// ============注入cdn end============
},
devServer: {
host: "localhost.longfor.com",
allowedHosts: [".longfor.com"],
port: 9001,
https: true,
proxy: {
"/psbr-board-server-uat": {
target: process.env.VUE_APP_BASE_URL,
changeOrigin: true,
},
},
},
css: {
loaderOptions: {
stylus: {
'resolve url': true,
import: [path.join(__dirname, './src/style/variable.styl')]
},
postcss: {
autoprefixer: {},
plugins: [
require('postcss-px2rem')({
remUnit: '37.5',
propList: ['*']
})
]
}
}
},
2、SvgIcon.vue
<template>
<svg :class="svgClass" aria-hidden="true" v-on="$listeners">
<use :xlink:href="iconName" :style="{ fill: color }" />
</svg>
</template>
<script>
// 使用方式: doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usage
export default {
name: 'SvgIcon',
props: {
iconClass: {
type: String,
required: true
},
className: {
type: String,
default: ''
},
color: {
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>
3、配置
// svg组件 用于常用图标引入
import SvgIcon from '@/components/SvgIcon'
Vue.component('svg-icon', SvgIcon)
const req = require.context('@/icons/svg', false, /\.svg$/)
const requireAll = (requireContext) => requireContext.keys().map(requireContext)
requireAll(req)
4、应用
// info就是svg文件名称
<SvgIcon icon-class="info" class="info"></SvgIcon>