1.簡單上手應用
一、在阿里巴巴矢量圖標庫官網里面創建自己的項目,在里面添加自己項目得圖標。(具體如何新建項目添加圖標不是本文重點可自行百度)
二、點擊下載至本地
三、找到下載下來的iconfont.js復制
四、引進項目
好了,到這里就完了!!!看效果
需要注意的一個地方:#icon-其中icon-是你在新建項目的時候設置的前綴
就是上面框住的地方,當然這只是一個簡單的demo例子
引進項目里面你可以參考一下這篇文章 :https://blog.csdn.net/xiaocn325/article/details/100153307 建議一定要讀完回來再看下面的內容
仔細看完你會發現重點來了
按照這樣的方法在項目里面可以使用svg圖但是有兩個步驟在不斷重復,這對於ui跟新圖標庫前端引用來說都是極其的不方便,因此就需要將其封裝成組件,下一次需要添加的時候直接現在svg文件引入一個文件就可以了,不需要頻繁替換iconfont,js,團隊協作也很方便;
2.使用svg格式字體圖標在vue項目里的封裝
1、第一步:安裝解析svg類型圖標的依賴庫
npm install svg-sprite-loader --save-dev
2、配置vue.config.js文件,代碼如下 我現在用的webpack是4.0以上版本的 這一步配置很關鍵,這里配置失敗圖標是出不來的,如果有報resovle is undefined 則
chainWebpack(config) { // set svg-sprite-loader config.module .rule('svg') .exclude.add(resolve('src/icons')) .end() config.module .rule('icons') .test(/\.svg$/) .include.add(resolve('src/icons')) .end() .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'icon-[name]' }) .end() }
整個文件如下
const path = require('path') module.exports = { publicPath:'./' , devServer: { proxy: { '/api':{ target: 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg', // target: 'http://192.168.3.20:8154/', changeOrigin: true, pathRewrite: { '^/api': '/' } }, }, disableHostCheck: true }, chainWebpack: config => { config.entry('main').add('babel-polyfill') // main是入口js文件 // 其他配置 config.module .rule('svg') .exclude.add(path.resolve(__dirname,"src/icons")) .end() config.module .rule('icons') .test(/\.svg$/) .include.add(path.resolve(__dirname,"src/icons")) .end() .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'icon-[name]' }) .end() }, lintOnSave: false, };
3、在src/components下新建文件夾及文件SvgIcon/index.vue,代碼如下
<template> <!--<svg class="svgClass" aria-hidden="true"> <use :xlink:href="iconName"></use> </svg>--> <div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$listeners" /> <svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners"> <use :xlink:href="iconName" /> </svg> </template> <script> // doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usage import { isExternal } from '@/utils/validate' export default { name: 'SvgIcon', props: { iconClass: { type: String, required: true }, className: { type: String, default: '' } }, computed: { isExternal() { // return false return isExternal(this.iconClass) }, iconName() { return `#icon-${this.iconClass}` }, svgClass() { if (this.className) { return 'svg-icon ' + this.className } else { return 'svg-icon' } }, styleExternalIcon() { return { mask: `url(${this.iconClass}) no-repeat 50% 50%`, '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%` } } } } </script> <style scoped> .svg-icon { width: 1em; height: 1em; vertical-align: -0.15em; fill: currentColor; overflow: hidden; } .svg-external-icon { background-color: currentColor; mask-size: cover!important; display: inline-block; } </style>
4、在src下新建icons文件夾,及icons文件夾下svg文件夾、index.js文件、svgo.yml文件
index.js文件
import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg component
// register globally
Vue.component('svg-icon', SvgIcon)
const req = require.context('./svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
看不懂這段代碼的可以參考這個來鏈接 https://blog.csdn.net/qq_21567385/article/details/107626075
svgo.yml文件
# replace default config # multipass: true # full: true plugins: # - name # # or: # - name: false # - name: true # # or: # - name: # param1: 1 # param2: 2 - removeAttrs: attrs: - 'fill' - 'fill-rule'
5、svg文件夾下面放svg圖標文件
6、在main.js中引入svg
import '@/icons'
7、配置package.json文件
"svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml"
8、當初始化使用或者新增svg圖標時,需要執行以下代碼
npm run svgo
不知道svgo是干用的可以參考這篇文章 https://www.cnblogs.com/dongxiaolei/p/7344662.html
9、使用svg圖標
其中“chaxun”為svg文件中svg圖標的名稱
到這里就結束了。
如果你的svg圖沒有出來可以留言交流哦 一起探討技術問題