vue2 系列:SvgIcon 组件构建

该文章详细介绍了如何在Vue项目中创建一个自定义的SvgIcon组件,通过svg-sprite-loader处理SVG图标,将SVG文件集中管理并生成精灵图。同时,文章提到了在main.js中的引入以及在组件中使用SvgIcon的方式,并提醒了如果color属性无效可能的原因。

效果图

1.  组件编写 components/SvgIcon.vue

export default {
  name: "SvgIcon",
  props: {
    name: {
      type: String,
      required: true,
    },
    className: String,
    color: String,
    width: String,
    height: String,
  },
  computed: {
    iconName() {
      return `#icon-${this.name}`;
    },
    svgClass() {
      let className = this.className ? `icon-${this.className}` : "";
      return ["svg-icon", className];
    },
  },
  data() {
    return {};
  },
};
</script>
<template>
  <!-- aria-hidden: 让这个元素对浏览器隐藏 -->
  <svg
    :class="svgClass"
    :style="{ color, width, height }"
    aria-hidden="true"
    v-bind="$attrs"
  >
    <use :xlink:href="iconName" />
  </svg>
</template>

<style scoped>
.svg-icon {
  width: 1.3em;
  height: 1.3em;
  vertical-align: -0.3em;
  fill: currentColor;
  overflow: hidden;
}
</style>

2. 在 assets 下创建 icons/svg 文件夹, icons/index.js 文件

2.1 将 .svg 文件保存在 svg 文件夹下

2.2 icons/index.js 编写

const req = require.context('./svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)

3. 安装 svg 依赖 svg-sprite-loader

    安装 path 依赖 path-browserify

yarn add svg-sprite-loader
yarn add path-browserify
// or
npm install svg-sprite-loader
npm install path-browserify

4. vue.config.js 编写

const { defineConfig } = require("@vue/cli-service");
const path = require("path");

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = defineConfig({
  publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
  outputDir: "admin",
  assetsDir: "static",
  lintOnSave: false,
  configureWebpack: {
    resolve: {
      alias: {
        "@": resolve("src"),
      },
      fallback: {
        path: require.resolve("path-browserify")
      }
    },
  },
  chainWebpack(config) {
    config.module.rule("svg").exclude.add(resolve("src/assets/icons")).end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/assets/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]",
      })
      .end();
  }
});

5. main.js 编写

import Vue from 'vue'
import App from './App.vue'

Vue.config.productionTip = false

// svg
import "./assets/icons/index.js"; 

import SvgIcon from "@/components/SvgIcon.vue"

Vue.component('SvgIcon', SvgIcon)

new Vue({
  render: h => h(App),
}).$mount('#app')

6. 使用

<template>
  <div id="app">
    <!--  name: 必填 svg 的文件名
          className: 选填 class 类名
          color: 选填 颜色
          width: 选填 宽
          height: 选填 高  -->
    <svg-icon name="user" color="red" width="30" height="30"></svg-icon>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

7. 特别提示:如果 color 属性无效,需删除 .svg 文件里 fill="" 属性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yqcoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值