Vue3中级指南之如何在vite中使用svg图标详解

 更新时间:2022年04月28日 09:41:10   作者:只会番茄炒蛋  
在以webpack为构建工具的开发环境中我们可以很方便的实现SVG图标的组件化,下面这篇文章主要给大家介绍了关于Vue3中级指南之如何在vite中使用svg图标的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下

前言

svg图片在项目中使用的非常广泛,今天记录一下我是如何在vue3 + vite 中使用svg图标。

vite-plugin-svg-icons

  • 预加载 在项目运行时就生成所有图标,只需操作一次 dom
  • 高性能 内置缓存,仅当文件被修改时才会重新生成

安装

node version:  >=12.0.0 vite version:  >=2.0.0

yarn add vite-plugin-svg-icons -D
# or
npm i vite-plugin-svg-icons -D
# or
pnpm install vite-plugin-svg-icons -D

使用

  • vite.config.ts 中的配置插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'

export default () => {
  return {
    plugins: [
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/icons')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',

        /**
         * 自定义插入位置
         * @default: body-last
         */
        // inject?: 'body-last' | 'body-first'

        /**
         * custom dom id
         * @default: __svg__icons__dom__
         */
        // customDomId: '__svg__icons__dom__',
      }),
    ],
  }
}
  • 在 src/main.js 内引入注册脚本
import 'virtual:svg-icons-register'

如何在组件中使用

创建SvgIcon组件

/src/components/SvgIcon/index.vue

<template>
  <svg aria-hidden="true" class="svg-icon" :width="props.size" :height="props.size">
    <use :xlink:href="symbolId" rel="external nofollow"  :fill="props.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'
  },
  size: {
    type: String,
    default: '1em'
  }
})
const symbolId = computed(() => `#${props.prefix}-${props.name}`)
</script>

icons目录结构

# src/icons

- icon1.svg
- icon2.svg
- icon3.svg
- dir/icon1.svg

全局注册组件

# src/main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

import svgIcon from "@/components/SvgIcon/index.vue";
import 'virtual:svg-icons-register'

createApp(App)
    .use(ElementPlus)
    .use(router)
    .component('svg-icon', svgIcon)
    .mount('#app')

页面使用

<template>
    <svg-icon v-if="props.icon" :name="props.icon" />
    <span v-if="props.title" slot='title'>{{ props.title }}</span>
</template>

<script setup>
const props = defineProps({
    icon: {
        type: String,
        default: ''
    },
    title: {
        type: String,
        default: ''
    }
})
</script>

获取所有 SymbolId

import ids from 'virtual:svg-icons-names'
// => ['icon-icon1','icon-icon2','icon-icon3']

总结

到此这篇关于Vue3中级指南之如何在vite中使用svg图标的文章就介绍到这了,更多相关vite使用svg图标内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue3中v-model语法糖的三种写法详解

    Vue3中v-model语法糖的三种写法详解

    这篇文章主要为大家详细介绍了Vue3中v-model语法糖的三种写法,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • 基于Vue实现平滑过渡的拖拽排序功能

    基于Vue实现平滑过渡的拖拽排序功能

    这篇文章主要介绍了vue实现平滑过渡的拖拽排序功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-06-06
  • vue.js中created()与activated()的个人使用解读

    vue.js中created()与activated()的个人使用解读

    这篇文章主要介绍了vue.js中created()与activated()的个人使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • 基于vue实现swipe分页组件实例

    基于vue实现swipe分页组件实例

    本篇文章主要介绍了基于vue实现swipe分页组件实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • el-select 下拉框多选实现全选的实现

    el-select 下拉框多选实现全选的实现

    这篇文章主要介绍了el-select 下拉框多选实现全选的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • Vue组件教程之Toast(Vue.extend 方式)详解

    Vue组件教程之Toast(Vue.extend 方式)详解

    这篇文章主要给大家介绍了关于Vue组件教程之Toast(Vue.extend 方式)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • 使用vue-router切换页面时实现设置过渡动画

    使用vue-router切换页面时实现设置过渡动画

    今天小编就为大家分享一篇使用vue-router切换页面时实现设置过渡动画。具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-10-10
  • Vue无限滑动周选择日期的组件的示例代码

    Vue无限滑动周选择日期的组件的示例代码

    这篇文章主要介绍了Vue无限滑动周选择日期的组件的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • 学习Vue组件实例

    学习Vue组件实例

    本篇文章给大家分享了Vue实例的相关内容以及重要知识点,对此有兴趣的朋友可以跟着学习参考下。
    2018-04-04
  • Vue element树形控件添加虚线详解

    Vue element树形控件添加虚线详解

    这篇文章主要为大家介绍了Vue element树形控件添加虚线,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助<BR>
    2021-11-11

最新评论