vue中使用svg-icon遇到的坑及解決
1.每次在項(xiàng)目中使用icon的圖標(biāo)時(shí)
總是覺得引入很長(zhǎng)的路徑很麻煩,最近發(fā)現(xiàn)一個(gè)插件挺好用的,svg-sprite-loader
svg-sprite-loader實(shí)際上是把所有的svg打包成一張雪碧圖,
再通過<use xlink:href="#xxx"/>來(lái)顯示你所需的icon,
但是在使用的過程總會(huì)出現(xiàn)問題;
2.安裝依賴
npm i svg-sprite-loader --save
3.vue中vue.config.js中的配置
chainWebpack(config) { const svgRule = config.module.rule("svg"); // 清除已有的所有 loader。 svgRule.uses.clear(); svgRule .test(/\.svg$/) .include.add(path.resolve(__dirname, "src/assets/icons")) // 文件目錄 .end() .use("svg-sprite-loader") .loader("svg-sprite-loader") .options({ symbolId: "icon-[name]", }); const fileRule = config.module.rule("file"); fileRule.uses.clear(); fileRule .test(/\.svg$/) .exclude.add(path.resolve(__dirname, "src/assets/icons")) .end() .use("file-loader") .loader("file-loader"); },
或者只需要簡(jiǎn)單的配置:
chainWebpack(config) { 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(); },
以上的兩種試過都是可以的。
4.在src文件目錄下
新建一個(gè)icons文件,icons里面的svg文件是需要用到的svg矢量圖
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); requireAll(req);
5.在components下面新建一個(gè)SvgIcon文件
index.vue文件:
<template> <!-- <div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$listeners" /> --> <svg :class="svgClass" aria-hidden="true"> <use :xlink:href="iconName" rel="external nofollow" /> </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 isExternal(this.iconClass); }, iconName() { return `#icon-${this.iconClass}`; }, svgClass() { if (this.className) { console.log(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; color: #fff; } .svg-external-icon { background-color: currentColor; mask-size: cover !important; display: inline-block; } </style>
6.在main.js中引用就行
7.在需要用到的組件中使用
8.vue-cli2.0下找到build下webpack.base.config.js
在module中的rule下加入下面的規(guī)則:
{ test: /\.svg$/, loader: "svg-sprite-loader", include: [resolve("src/icons")], options: { symbolId: "icon-[name]" } },
如果存在file-loader的規(guī)則還需要更改路徑:
{ test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, loader: "url-loader", options: { limit: 10000, name: utils.assetsPath("img/[name].[hash:7].[ext]") }, exclude: [resolve("src/icons")]//加入這句 },
應(yīng)該就不會(huì)有什么問題了
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue使用Vue Elements實(shí)現(xiàn)文件預(yù)覽功能
在現(xiàn)代 web 開發(fā)中,用戶與系統(tǒng)的交互體驗(yàn)越來(lái)越重要,而文件上傳和文件預(yù)覽是最常見的交互場(chǎng)景之一,本文將詳細(xì)介紹如何在 Vue 項(xiàng)目中使用 Vue Elements 來(lái)實(shí)現(xiàn)文件預(yù)覽的功能,包括基本使用方法、常見實(shí)例、性能優(yōu)化以及樣式自定義等內(nèi)容,需要的朋友可以參考下2025-01-01ElementUI的this.$notify.close()調(diào)用不起作用的解決
本文主要介紹了ElementUI的this.$notify.close()調(diào)用不起作用的解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08使用Vue-Router 2實(shí)現(xiàn)路由功能實(shí)例詳解
vue-router 2只適用于Vue2.x版本,下面我們是基于vue2.0講的如何使用vue-router 2實(shí)現(xiàn)路由功能,需要的朋友可以參考下2017-11-11Vue快速實(shí)現(xiàn)通用表單驗(yàn)證的方法
這篇文章主要介紹了Vue快速實(shí)現(xiàn)通用表單驗(yàn)證的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02