在vue中使用svg
在vue中使用svg
- svg的好处
- 矢量性(无论图片放多大,都不会出现锯齿状模糊)
- 利于seo
- 步骤
- 1、安装依赖:
npm install svg-sprite-loader --save-dev
- 2、配置vue-cli
chainWebpack(config) { // 配置svg 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() }
- 3、在src/components/svgIcon下新建svg组件
<template> <svg :class="svgClass" aria-hidden="true" v-on="$listeners"> <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: 10em; height: 10em; vertical-align: -0.15em; fill: currentColor; overflow: hidden; } </style>
- 4、在src下新建icons文件夹,及icons文件夹下svg文件夹、index.js文件, index.js文件内容如下
import Vue from 'vue' import SvgIcon from '@/components/svg'// 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、在main.js中引入svg
import './assets/icons/index'
- 6下载svg图片将svg图片放在assets/icons/svg/下面
- 7、在页面中使用(可以通过font-size,color来设置svg图标颜色和大小,设置color时需要将svg文件中的path下面的fill=“#xxx”去除即可)
<div class="svg_box"> SVG图标 <svg-icon icon-class="smile"></svg-icon> <svg-icon icon-class="cry"></svg-icon> </div>
在Vue3 + vite中使用svg
由于vite的svg无法兼容vue2的版本,只能重新找如何实现,结果找了很久没有几个文章有用的要么打包警告,要么就是无法实现直接报错的,终于找到了官方文档是使用另一个插件实现的vite-plugin-svg-icons
安装插件
yarn add vite-plugin-svg-icons -D
或者
npm i vite-plugin-svg-icons -D
配置插件 vite.config.js
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { resolve } from 'path'
export default defineConfig({
plugins: [
createSvgIconsPlugin({
iconDirs: [resolve(process.cwd(), 'src/assets/svg')],
symbolId: 'icon-[dir]-[name]',
})
]
})
存放文件路径 /src/assets/icons
src/assets/icons
- icon1.svg
- icon2.svg
- icon3.svg
- dir/icon1.svg
然后就在main.js / main.ts 加入否则报错
import 'vite-plugin-svg-icons/register';
// 需要全局引入再添加
import svgIcon from './components/SvgIcon/index.vue' // 全局svg图标组件
app.component('svg-icon', svgIcon)
SvgIcon组件实现
<template> <svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <use :xlink:href="symbolId" :fill="color" /> </svg> </template> <script setup> import { computed } from 'vue' const props = defineProps({ prefix: { type: String, default: 'icon', }, name: { type: String, required: true, }, color: { type: String, default: '#333', }, }) const symbolId = computed(() => `#${props.prefix}-${props.name}`) </script> <style scope> .svg-icon { width: 1em; height: 1em; fill: currentColor; vertical-align: middle; } </style>
svg-icon组件使用方法
<template> <div> <svg-icon name='vue'> </div> </template> <script> import { defineComponent, computed } from 'vue'; import SvgIcon from './components/SvgIcon.vue'; export default defineComponent({ name: 'App', components: { SvgIcon }, }); </script>