vue使用svg,自定义svg-icon组件

src\icons\index.js

import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon' // svg组件

Vue.component('svg-icon', SvgIcon)

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

components/SvgIcon/index.vue

<!--
 * @Description: 文件描述
 * @Author: yx
 * @Date: 2023-05-09 10:54:09
 * @LastEditors: yx
 * @LastEditTime: 2023-05-09 17:29:52
-->

<template>
	<svg :class="svgClass" aria-hidden="true">
		<title v-if="title">{{title}}</title>
		<use :xlink:href="iconName"></use>
	</svg>
</template>

<script>
export default {
	name: 'svg-icon',
	props: {
		iconClass: {
			type: String,
			required: true,
		},
		className: {
			type: String
		},
		title: {
			type: String
		}
	},
	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: 1em;
	height: 1em;
	vertical-align: -0.15em;
	fill: currentColor;
	overflow: hidden;
}
</style>

main.js

import './icons'

使用:
在这里插入图片描述
其实此时还不能正确的显示出来,还需要经过如下配置,在vue.config.js文件夹下进行配置,添加如下信息:
在这里插入图片描述

    config.module.rule('svg').exclude.add(resolve('src/icons/svg')).end()
    config.module.rule('icons').test(/\.svg$/)
      .include.add(resolve('src/icons/svg')).end()
      .use('svg-sprite-loader').loader('svg-sprite-loader').options({
        symbolId: 'icon-[name]'
      }).end()

发现还是不行,只需要安装依赖:

npm i svg-sprite-loader --save

然后就可以使用了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值