引入
- public.html 引入在线链接
- 下载到本地后main.js 引入 iconfont.js

注意:如果项目开了eslint,可能引入iconfont.js会报错,要设置忽略

使用
<svg class="icon" aria-hidden="true">
<use xlink:href="#icons-PromptInformation-solid"></use>
</svg>
webpack 识别svg
chainWebpack: (config) => {
const svgRule = config.module.rule('svg')
svgRule.uses.clear()
svgRule
.oneOf('inline')
.resourceQuery(/inline/)
.use('vue-svg-icon-loader')
.loader('vue-svg-icon-loader')
.end()
.end()
.oneOf('external')
.use('file-loader')
.loader('file-loader')
.options({
name: 'assets/[name].[hash:8].[ext]'
})
}
组件封装
// IconSvg.vue
<template>
<div>
<svg class="icon" aria-hidden="true">
<use :xlink:href="iconHref"></use>
</svg>
<slot></slot>
</div>
</template>
<script>
export default {
name: 'iconSvg',
props: {
iconClass: {
type: String,
required: true
}
},
computed: {
iconHref: function () {
return `#${this.iconClass}`
}
}
}
</script>
<style scoped>
.icon {
width: 2em;
height: 2em;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}
</style>
全局组件注册
// 引入
import IconSvg from '@/components/IconSvg/IconSvg'
const globalComponents = {
// 注册
install (Vue) {
Vue.component('IconSvg', IconSvg)
}
}
export default globalComponents
// main.js
import globalComponents from './components/' // global components
Vue.use(globalComponents)`在这里插入代码片`
组件使用
<icon-svg :iconClass="'icona-project-solid'"></icon-svg>