广告:宝塔Linux面板高效运维的服务器管理软件 点击【 https://www.bt.cn/p/uNLv1L 】立即购买

此处还需要安装下fast-glob相关依赖,不然vite运行npm run dev时会报Cannot find module 'fast-glob’的错误
npm i fast-glob@3.x -Dnpm i vite-plugin-svg-icons@2.x -D登录后复制二、在src/components/svgIcon下新建组件index.vue
<template>  <svg aria-hidden="true" class="svg-icon">    <use :xlink:href="symbolId" rel="external nofollow"  :fill="color" />  </svg></template><script setup lang="ts">import { computed } from 'vue';const props = defineProps({  prefix: {type: String,default: 'icon',},  iconClass: {type: String,required: true,},  color: {type: String,default: ''}})const symbolId = computed(() => `#${props.prefix}-${props.iconClass}`);</script><style scoped>.svg-icon {  width: 1em;  height: 1em;  vertical-align: -0.15em;  overflow: hidden;  fill: currentColor;}</style>登录后复制三、tsconfig.json中添加设置types用来指定需要包含的模块,只有在这里列出的模块的声明文件才会被加载进来。非必要添加,我在两个demo测试的时候,一个需要一个不需要,若有问题可以尝试添加
{  "compilerOptions": {    "types": ["vite-plugin-svg-icons/client"]  }}登录后复制四、vite.config.ts 中的配置插件import { resolve } from 'path'import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'export default defineConfig({  plugins: [    createSvgIconsPlugin({      // 指定需要缓存的图标文件夹      iconDirs: [resolve(process.cwd(), 'src/assets/imgs/svg')],      // 指定symbolId格式      symbolId: 'icon-[dir]-[name]',    })  ]})登录后复制五、在main.ts全局注册组件import { createApp } from 'vue'import App from './App.vue'import router from '@/router'import { store, key } from '@/store'const app = createApp(App)import 'virtual:svg-icons-register' // 引入注册脚本import SvgIcon from '@/components/svgIcon/index.vue' // 引入组件app.component('svg-icon', SvgIcon)app.use(router).use(store, key).mount('#app')登录后复制六、在页面中使用<template> <svg-icon icon-class="category"></svg-icon> <svg-icon icon-class="accountant" ></svg-icon></template>登录后复制七、文件目录结构及其效果展示
以上就是vue3+vite2中怎么使用svg方法的详细内容,更多请关注9543建站博客其它相关文章!
 
            
 
         
                    .jpg) 
                     
                     
                    
发表评论