vue中使用svg-icon遇到的坑

1.每次在项目中使用icon的图标时,总是觉得引入很长的路径很麻烦,最近发现一个插件挺好用的,svg-sprite-loader

svg-sprite-loader实际上是把所有的svg打包成一张雪碧图,
再通过<use xlink:href="#xxx"/>来显示你所需的icon,

但是在使用的过程总会出现问题;

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");
  },

或者只需要简单的配置:

 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文件目录下,新建一个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下面新建一个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" />
  </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下加入下面的规则:

{
        test: /\.svg$/,
        loader: "svg-sprite-loader",
        include: [resolve("src/icons")],
        options: {
          symbolId: "icon-[name]"
        }
},

如果存在file-loader的规则还需要更改路径:

 {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: "url-loader",
        options: {
          limit: 10000,
          name: utils.assetsPath("img/[name].[hash:7].[ext]")
        },
        exclude: [resolve("src/icons")]//加入这句
      },

应该就不会有什么问题了

vue2-svg-icon 是一个 Vue.js 的 SVG 图标组件,可以用于快速加载和使用 SVG 图标。以下是使用步骤: 1. 安装 vue2-svg-icon 可以使用 npm 或 yarn 进行安装: ```bash npm install vue2-svg-icon --save ``` 或者 ```bash yarn add vue2-svg-icon ``` 2. 引入组件 在你的 Vue.js 组件中引入 vue2-svg-icon: ```js import SvgIcon from 'vue2-svg-icon' Vue.component('svg-icon', SvgIcon) ``` 3. 使用组件 在模板中使用 `svg-icon` 标签,并指定 `icon` 属性为对应的图标名称: ```html <svg-icon icon="arrow-up"></svg-icon> ``` 其中 `arrow-up` 是图标的名称,具体的图标名称需要查看你所使用SVG 图标库。 4. 配置 SVG 图标库 vue2-svg-icon 默认使用的是 [icomoon](https://icomoon.io/) 的 SVG 图标库,你可以在 `SvgIcon` 组件中通过 `set` 方法来配置使用其他的 SVG 图标库。 例如,如果你想使用 [fontawesome](https://fontawesome.com/) 的 SVG 图标库,可以这样配置: ```js import SvgIcon from 'vue2-svg-icon' import { library } from '@fortawesome/fontawesome-svg-core' import { faCoffee } from '@fortawesome/free-solid-svg-icons' import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome' library.add(faCoffee) Vue.component('font-awesome-icon', FontAwesomeIcon) SvgIcon.set({ name: 'fontawesome', classPrefix: 'fa-', classSuffix: '', defaultWidth: '1em', defaultHeight: '1em' }) ``` 这里我们使用了 `@fortawesome/fontawesome-svg-core` 和 `@fortawesome/free-solid-svg-icons` 来引入 fontawesome 的 SVG 图标库,并且配置了 `SvgIcon` 组件使用 `fontawesome` 图标库,并指定了图标的 class 前缀为 `fa-`,这样我们就可以在模板中使用 `fa-coffee` 这个图标了: ```html <svg-icon icon="fa-coffee"></svg-icon> ``` 更多关于 vue2-svg-icon使用方法,可以参考它的官方文档:https://github.com/cenkai88/vue-svg-icon#readme
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值