vue中使用svg icon

1、安装svg-sprite-loader

npm install svg-sprite-loader
#OR
yarn add svg-sprite-loader

2、封装svgicon组件

在/src/components/下创建组件SvgIcon

/src/components/SvgIcon/index.vue

<template>
    <svg :class="svgClass" aria-hidden="true">
        <use :xlink:href="iconName" />
    </svg>
</template>
  
  <script>
export default {
    name: "SvgIcon",
    props: {
        iconClass: {
            type: String,
            required: true,
        },
        className: {
            type: String,
            default: "",
        },
    },
    computed: {
        iconName() {
            return `#icon-${this.iconClass}`;
        },
        svgClass() {
            if (this.className) {
                return "svg-icon " + this.className;
            } else {
                return "svg-icon";
            }
        },
    },
};
</script>
  
  <style scoped>
.svg-icon {
    width: 1rem;
    height: 1rem;
    vertical-align: -0.15em;
    fill: currentColor;
    overflow: hidden;
}
</style>

3、注册svg组件

在src文件夹下创建icons文件夹,在该文件夹下创建svg文件夹和index.js

svg文件夹:存放svg图标

index.js:注册svg组件

/src/icons/index.js

import Vue from "vue";
import SvgIcon from "@components/SvgIcon"; // 对应上一步骤中的组件位置,注意@components需要在vue.config.js中配置别名

// 全局注册svg组件
Vue.component("svg-icon", SvgIcon);
// 工程化导入svg图片
const req = require.context("./svg", false, /\.svg$/);
// 定义一个加载目录的函数
const requireAll = (requireContext) =>
    requireContext.keys().map(requireContext);
// 加载目录下的所有svg文件
requireAll(req);

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({
    configureWebpack: {
        resolve: {
            alias: {
                // 配置路径别名
                "@": resolve("src"),
                "@components": resolve("src/components"),
            },
        },
    },
    chainWebpack(config) {
        // 禁用系统内部对svg的处理
        config.module.rule("svg").exclude.add(resolve("src/icons")).end();
        // 新建规则处理svg文件
        config.module
            .rule("icons")
            .test(/\.svg$/) //添加匹配规则
            .include.add(resolve("src/icons")) //添加我们要处理的文件路径
            .end()//上面的add方法改变了上下文,调用end()退回到include这一级
            .use("svg-sprite-loader")//使用"svg-sprite-loader"依赖
            .loader("svg-sprite-loader")//选中这个依赖
            .options({
                symbolId: "icon-[name]",// 这个配置是这个包的配置不属于webpack,可以查看相关文档,symbolId指定我们使用svg图片的名字
            })
            .end();
    },
});

5、main.js引入svg

import Vue from "vue";
import App from "./App.vue";


Vue.config.productionTip = false;

// 引入svg
import "./icons";

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

6、使用

在/src/icons/svg/存放需要的svg图标

在页面中使用

<template>
    <div>
        <!-- icon-class对应“/src/icons/svg/”下的文件名;class-name为自定义的类名 -->
        <svg-icon icon-class="search" class-name="icon" />
    </div>
</template>

<style scoped>
.icon {
    width: 100px;
    height: 100px;
}
</style>

补充:

1、demo可在iconfont-阿里巴巴矢量图标库中搜索图标,然后下载svg格式使用

<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M14 9C14 11.7614 11.7614 14 9 14C6.23858 14 4 11.7614 4 9C4 6.23858 6.23858 4 9 4C11.7614 4 14 6.23858 14 9ZM13.5815 12.8744C14.4664 11.8291 15 10.4769 15 9C15 5.68629 12.3137 3 9 3C5.68629 3 3 5.68629 3 9C3 12.3137 5.68629 15 9 15C10.4769 15 11.8291 14.4664 12.8744 13.5815L17.6464 18.3536L18.3536 17.6464L13.5815 12.8744Z" fill="currentColor"/>
</svg>

2、自定义icon颜色,设置color即可

<style scoped>
.icon {
    width: 100px;
    height: 100px;
    color: rgb(127, 215, 235);
}
</style>

3、若自定义颜色没有生效,在svg文件中搜索fill,将设置颜色的fill更改为currentColor,

如下示例中有一个fill="black",将其改为currentColor

原始svg:

<svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M10.5 4.35427C10.5 5.09225 10.3094 5.81708 9.93404 6.45283C9.24773 7.61515 7.2279 10.318 6.35986 11.4674C6.15844 11.7341 5.759 11.7297 5.56295 11.459C4.71649 10.2903 2.74226 7.54629 2.06043 6.4408C1.6749 5.81572 1.5 5.08771 1.5 4.35427C1.5 1.95324 3.51853 0 6.00043 0C8.48119 0 10.5 1.95324 10.5 4.35427ZM2.0665 4.35427C2.0665 5.03176 2.24033 5.70159 2.60706 6.27299C3.37601 7.47105 5.98184 10.9847 5.98184 10.9847C5.98184 10.9847 8.65401 7.52275 9.42503 6.21487C9.75789 5.65022 9.9335 5.00929 9.9335 4.35427C9.9335 2.25569 8.16859 0.548187 6.00043 0.548187C3.83165 0.548187 2.0665 2.25569 2.0665 4.35427ZM4 4.49999C4 3.39663 4.89716 2.5 6.00028 2.5C7.10282 2.5 8.00001 3.39665 8 4.49999C8 5.60286 7.10281 6.5 6.00028 6.5C4.89716 6.5 4 5.60286 4 4.49999ZM4.54783 4.49999C4.54783 5.30086 5.19887 5.9522 6.00028 5.9522C6.80058 5.9522 7.45216 5.30087 7.45215 4.49999C7.45215 3.69914 6.80058 3.04778 6.00028 3.04778C5.19887 3.04778 4.54783 3.69914 4.54783 4.49999Z" fill="black"/>
</svg>

更改后:

<svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M10.5 4.35427C10.5 5.09225 10.3094 5.81708 9.93404 6.45283C9.24773 7.61515 7.2279 10.318 6.35986 11.4674C6.15844 11.7341 5.759 11.7297 5.56295 11.459C4.71649 10.2903 2.74226 7.54629 2.06043 6.4408C1.6749 5.81572 1.5 5.08771 1.5 4.35427C1.5 1.95324 3.51853 0 6.00043 0C8.48119 0 10.5 1.95324 10.5 4.35427ZM2.0665 4.35427C2.0665 5.03176 2.24033 5.70159 2.60706 6.27299C3.37601 7.47105 5.98184 10.9847 5.98184 10.9847C5.98184 10.9847 8.65401 7.52275 9.42503 6.21487C9.75789 5.65022 9.9335 5.00929 9.9335 4.35427C9.9335 2.25569 8.16859 0.548187 6.00043 0.548187C3.83165 0.548187 2.0665 2.25569 2.0665 4.35427ZM4 4.49999C4 3.39663 4.89716 2.5 6.00028 2.5C7.10282 2.5 8.00001 3.39665 8 4.49999C8 5.60286 7.10281 6.5 6.00028 6.5C4.89716 6.5 4 5.60286 4 4.49999ZM4.54783 4.49999C4.54783 5.30086 5.19887 5.9522 6.00028 5.9522C6.80058 5.9522 7.45216 5.30087 7.45215 4.49999C7.45215 3.69914 6.80058 3.04778 6.00028 3.04778C5.19887 3.04778 4.54783 3.69914 4.54783 4.49999Z" fill="currentColor"/>
</svg>

  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
vue2-svg-icon 是一个 Vue.jsSVG 图标组件,可以用于快速加载和使用 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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值