vue2使用svg

本文介绍如何在Vue项目中通过npm安装并配置SVG图标加载器,创建SvgIcon组件实现SVG图标的动态加载与显示,并通过index.js注册组件及获取图标列表。

1.下载模块

npm install --save-dev svg-sprite-loader

2.在components下新建SvgIcon.vue文件:

<template>
  <svg :class="getClassName" :width="width" :height="height" aria-hidden="true" v-on="$listeners">
    <use :xlink:href="getName"></use>
  </svg>
</template>

<script>
export default {
  name: 'icon-svg',
  props: {
    name: {
      type: String,
      required: true
    },
    className: {
      type: String
    },
    width: {
      type: String
    },
    height: {
      type: String
    }
  },
  computed: {
    getName() {
      return `#icon-${this.name}`
    },
    getClassName() {
      return ['icon-svg', `icon-svg__${this.name}`, this.className && /\S/.test(this.className) ? `${this.className}` : '']
    }
  }
}
</script>

<style>
.icon-svg {
  width: 1rem;
  height: 1rem;
  fill: currentColor;
  overflow: hidden;
}
</style>

3.src下新建icons文件夹,放svg文件和 index.js如下:
svg文件命名以icon-aaa形式
index.js文件如下:

import Vue from 'vue'
import IconSvg from '../components/SvgIcon.vue'

Vue.component('IconSvg', IconSvg)

const svgFiles = require.context('./svg', true, /\.svg$/)
const iconList = svgFiles.keys().map(item => svgFiles(item))
console.log(svgFiles,iconList);
export default {
  getNameList () {
    return iconList.map(item => item.default.id.split('-')[1])
  }
}

4.使用

<icon-svg name="aaa" class="icon"></icon-svg>
<style>
	.icon{
		width:100px;
		height:100px;
		color:red;
	}
</style>

5.如果不报错,图形没出来,可能需要编译插件

npm install babel-polyfill

6.在build下的webpack.base.conf.js文件中,entry改为:

entry: {
    app: ['babel-polyfill', './src/main.js']
 },

7.核查目录
在这里插入图片描述

<think>好的,我现在要解决用户的问题:在Vue3项目中如何使用SVG。首先,我需要回想一下Vue3的相关知识以及SVG使用方法。 用户已经提供了一些引用内容,主要是关于Vite配置、ESLint和Prettier的设置,虽然这些和SVG使用没有直接关联,但可能涉及项目配置,需要注意是否有影响。比如,Vite的配置文件可能需要添加SVG相关的处理。 首先,SVGVue中的使用通常有两种方式:直接作为组件导入,或者作为图片资源引用。Vue3和Vite结合时,可能需要使用特定的插件来处理SVG文件,比如vite-plugin-svg-icons或vite-svg-loader。这些插件可以帮助将SVG转换为Vue组件,方便使用。 接下来,我需要考虑安装和配置这些插件的步骤。例如,安装vite-plugin-svg-icons,然后在vite.config.ts中添加插件配置。同时,需要创建一个全局组件来自动导入SVG,这样在项目中就可以直接通过组件名来使用SVG图标了。 另外,可能还需要配置TypeScript,确保它能够识别.svg文件作为模块,这可以通过在env.d.ts文件中添加声明来实现。否则,TypeScript可能会报找不到模块的错误。 然后,用户可能需要知道如何具体使用这些SVG组件,比如在模板中通过组件名称调用,或者动态导入不同的SVG。同时,可能需要提到如何修改SVG的颜色和大小,通过CSS或属性传递。 还要注意,如果用户希望将SVG作为图片使用,比如在img标签的src属性中引用,需要确保路径正确,并且Vite能够正确处理这些静态资源。不过,作为组件导入的方式更灵活,推荐使用这种方式。 最后,可能需要提醒用户关于性能优化的问题,比如使用雪碧图或者按需加载,但可能这些属于进阶内容,可以在相关问题中提到。 现在需要检查是否有遗漏的步骤,比如是否需要配置ESLint或Prettier来处理SVG文件中的语法,不过通常这些工具不需要额外配置。另外,确保引用的插件是最新版本,并且与Vue3和Vite兼容。 总结步骤应该是:安装插件、配置Vite、创建全局组件、TypeScript声明、使用示例。然后生成相关问题,比如优化、动态使用、雪碧图等。</think>在Vue3项目中使用SVG可通过以下步骤实现,结合Vite构建工具进行配置: ### 一、安装SVG处理插件 推荐使用`vite-plugin-svg-icons`插件: ```bash npm install vite-plugin-svg-icons -D # 或 yarn add vite-plugin-svg-icons -D ``` ### 二、配置vite.config.ts ```typescript import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import { createSvgIconsPlugin } from 'vite-plugin-svg-icons' import path from 'path' export default defineConfig({ plugins: [ vue(), createSvgIconsPlugin({ iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')], // 指定SVG图标目录 symbolId: 'icon-[dir]-[name]' // 定义图标ID格式[^2] }) ] }) ``` ### 三、创建全局SVG组件 在`src/components`下创建`SvgIcon.vue`: ```vue <template> <svg aria-hidden="true" class="svg-icon"> <use :xlink:href="symbolId" /> </svg> </template> <script setup lang="ts"> import { computed } from 'vue' const props = defineProps({ name: { type: String, required: true } }) const symbolId = computed(() => `#icon-${props.name}`) </script> ``` ### 四、TypeScript类型声明 在`src/env.d.ts`中添加: ```typescript declare module '*.svg' { import type { DefineComponent } from 'vue' const component: DefineComponent export default component } ``` ### 五、使用示例 ```vue <template> <!-- 使用svg图标 --> <SvgIcon name="home" class="w-6 h-6 text-blue-500" /> </template> <script setup> import SvgIcon from '@/components/SvgIcon.vue' </script> ``` ### 关键配置说明: 1. **路径映射**:通过`resolve.alias`配置`@`指向src目录,确保路径引用正确[^1] 2. **插件机制**:Vite插件系统支持通过`createSvgIconsPlugin`实现SVG雪碧图优化 3. **按需加载**:自动将指定目录下的SVG文件转换为Symbol Sprite
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值